Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Okay, so I know this is probably a stupid question, but in my "NewForm.h" header file in Qt 5, I have the following under "Public":

C++
MainWindow::MainWindow *mw = new MainWindow::MainWindow();


With my main window class being "MainWindow", obviously. Its header file (which is imported in "NewForm.h", looks like this:

C++
#ifndef MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPropertyAnimation>
#include <QMessageBox>
#include <QGraphicsOpacityEffect>
#include <NewForm.h>
#include <QFormLayout>
#include <QSequentialAnimationGroup>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:

    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H


Yet, when I try to compile, I get:

error: C2653: 'MainWindow' : is not a class or namespace name
error: C2143: syntax error : missing ';' before '*'
error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error: C2653: 'MainWindow' : is not a class or namespace name
error: C2061: syntax error : identifier 'MainWindow'
error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error: C2864: 'NewForm::mw' : only static const integral data members can be initialized within a class
error: C1903: unable to recover from previous error(s); stopping compilation


I'm obviously doing something wrong... I come from a .NET (C#) background, and as far as I was aware, such an action should be possible in C++, no? I'm sure it's something little, and again, forgive me for the stupid question, but could someone please give me a hand? Thanks in advance! It's much appreciated.
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jul-13 15:45pm    
Why global variable? Having a global variable is a bad thing and is not usually required. Even if you want a singleton, use the proper implementation of singleton pattern.
—SA
RadXPictures 11-Jul-13 17:35pm    
Well, all I'm trying to do is be able to execute a function in MainWindow from NewForm, and following the approach MainWindow *mw = qobject_cast<mainwindow*>(this->parentWidget()->parentWidget()) didn't seem to work for me. NewForm is a widget, so I figured I would declare a global variable and assign my open MainWindow to it before the widget is printed to the screen. What other option do I have?
Sergey Alexandrovich Kryukov 11-Jul-13 21:22pm    
Of course: you should create an instance of MainWindow where you need it and pass to instances of other classes which use it. Global variables is evil and not really needed.
—SA
karimkarimkarim 10-Jul-13 16:18pm    
i think this is the solution: define "class MainWindow" before "namespace Ui" you must define a class then use it.
what is the Q_OBJECT in your code? i don't understand it
The_Inventor 11-Jul-13 2:40am    
What is Qt?

If you are using a C++ complier ;-) then you need to write in C++. There no givens, typos are not allowed, quit using commonly used, protected, class names or default names already used. Be more original.

C++
//The short answer is the line below needs to be in a .cpp file not a .h file
MainWindow::MainWindow *mw = new MainWindow::MainWindow();

//The following is longer break down of the errors.  I may have gone overboard.

//error: C2653: 'MainWindow' : is not a class or namespace name is referring to the first 'MainWindow' before the '::'
//error: C2143: syntax error : missing ';' before '*' is referring the the '*' that is after a second 'MainWindow' just after the '::'
//error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
//this is telling you the 'mw' is not understood by the complier nor is it going to help you this time, because when it did before, all kinds of virus got in the cracks.
//error: C2653: 'MainWindow' : is not a class or namespace name  
//sorry your: 

//#ifndef MAINWINDOW_H
//#ifndef MAINWINDOW_H
//#define MAINWINDOW_H
//#include <qmainwindow>

//didn't work, maybe because it is already used by the OS.

//error: C2061: syntax error : identifier 'MainWindow' well, here it got by the '=', and the 'new', but now your scope of a scope of a scope may be an issue.
//error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int it didn't like the other half either, 'MainWindow()';
//error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
//see above
//error: C2864: 'NewForm::mw' : only static const integral data members can be initialized within a class  As it says, only in the .CPP file, not the .H file.
//error: C2864: 'NewForm::mw' : only static const integral data members can be initialized within a class  How did this get in here?


Are you sure you are giving us full compliance?
 
Share this answer
 
v2
Comments
RadXPictures 11-Jul-13 17:37pm    
Thanks. But, for some reason, if I put that line into my .cpp, I can no longer access it from my other forms. It's the header file that makes it global, right? Because you have to use #include to gain access to the namespaces, classes, and functions in the cpp file.
The_Inventor 12-Jul-13 2:31am    
Variables declared in the main.h file, in the correct place, are considered to be 'global' to your application. In C++, variables and such, are declared in the *.h file for the class, and then initialized in the *.cpp file for said class.
Here is what you have shown. I'll add comments and the changes and then see what, if any, difference that it makes.

C++
//#ifndef MAINWINDOW_H
//#ifndef MAINWINDOW_H //Error you duplicated the above line
//#define MAINWINDOW_H
//Possible changes may help.
#ifdef MAINWINDOW_H
#undef MAINWINDOW_H
#define MAINWINDOW_H
//? What is Qt?

#include <QMainWindow>
#include <QPropertyAnimation>
#include <QMessageBox>
#include <QGraphicsOpacityEffect>
#include <NewForm.h>
#include <QFormLayout>
#include <QSequentialAnimationGroup>

namespace Ui {class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT// pretty sure you needed a ';'

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:

    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H


Below is my modified version of what I think may be your issues.

C++
#if !defined(MAINWINDOW_H__F188ACDC_1956_4C5B_9A04_CBFEBD566608__INCLUDED_)
#define MAINWINDOW_H__F188ACDC_1956_4C5B_9A04_CBFEBD566608__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif

#define Q_OBJECT
#include <qmainwindow.h>
#include <QPropertyAnimation.h>
#include <QMessageBox.h>
#include <QGraphicsOpacityEffect.h>
#include <NewForm.h>
#include <QFormLayout.h>
#include <QSequentialAnimationGroup.h>

/*  This more akin to C# than C++, however it shows a proper usage of 'using' and 'namespace'
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(Namespace = "Microsoft.WCF.Documentation")]
  interface IMessagingHello
  {
    [OperationContract(
     Action = "http://GreetingMessage/Action",
     ReplyAction = "http://HelloResponseMessage/Action"
    )]
    HelloResponseMessage Hello(HelloGreetingMessage msg);
  }

  class MessagingHello : IMessagingHello
  {
    public HelloResponseMessage Hello(HelloGreetingMessage msg)
    {
      Console.WriteLine("Caller sent: " + msg.Greeting);
      HelloResponseMessage responseMsg = new HelloResponseMessage();
      responseMsg.Response = "Service received: " + msg.Greeting;
      responseMsg.ExtraValues = String.Format("Served by object {0}.", this.GetHashCode().ToString());
      Console.WriteLine("Returned response message.");
      return responseMsg;
    }
  }
}
*/
namespace Ui
{
   class CMainWindow : public QMainWindow
   {
      Q_OBJECT = NULL;

    public:
    explicit CMainWindow(QWidget *parent = 0);
    ~CMainWindow();

    private:

    Ui::CMainWindow *ui;

   }
   CMainWindow* mw;

} // This will declare the Ui namespace, presuming it is fleshed out better.
//class MainWindow; This works a forward class declaration, but is not needed here.

#endif // MAINWINDOW_H

</qmainwindow.h>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900