Forward Declaration error
Page |
1 |
Hi All,
I am continuously getting an error no matter how I tried. It says:
/home/pragati/MultiFuncTester/Components/DMM/Build/../Source/DMM.cpp:-1: In constructor ‘DMM::DMMMenu::DMMMenu(QWidget*)’:
1. /home/pragati/MultiFuncTester/Components/DMM/Build/../Source/DMM.cpp:9: error: invalid use of incomplete type ‘struct DMM::Ui::DMMMenu’
2./home/pragati/MultiFuncTester/Components/DMM/Build/../Include/DMM.h:11: error: forward declaration of ‘struct DMM::Ui::DMMMenu’
Here is my DMM.h
- #ifndef DMM_H
- #define DMM_H
- #include <QtGui>
- #include <QWidget>
- namespace DMM {
- namespace Ui {
- class DMMMenu;
- }
- {
- Q_OBJECT
- public:
- ~DMMMenu();
- private:
- Ui::DMMMenu *ui;
- };
- }
- #endif // DMM_H
Here is my DMM.cpp
- # include <../Include/DMM.h>
- # include <ui_DMM.h>
- namespace DMM {
- ui(new Ui::DMMMenu)
- {
- ui->setupUi(this);
- }
- }
And this is the DMM_ui.h
- /********************************************************************************
- ** Form generated from reading UI file 'DMM.ui'
- **
- ** Created: Fri May 18 10:47:28 2012
- ** by: Qt User Interface Compiler version 4.8.0
- **
- ** WARNING! All changes made in this file will be lost when recompiling UI file!
- ********************************************************************************/
- #ifndef UI_DMM_H
- #define UI_DMM_H
- #include <QtCore/QVariant>
- #include <QtGui/QAction>
- #include <QtGui/QApplication>
- #include <QtGui/QButtonGroup>
- #include <QtGui/QHeaderView>
- #include <QtGui/QPushButton>
- #include <QtGui/QWidget>
- QT_BEGIN_NAMESPACE
- class Ui_DMM
- {
- public:
- {
- if (DMM->objectName().isEmpty())
- DMM->resize(360, 204);
- sizePolicy.setHorizontalStretch(0);
- sizePolicy.setVerticalStretch(0);
- sizePolicy.setHeightForWidth(DMM->sizePolicy().hasHeightForWidth());
- DMM->setSizePolicy(sizePolicy);
- retranslateUi(DMM);
- } // setupUi
- {
- } // retranslateUi
- };
- namespace Ui {
- class DMMMenu: public Ui_DMM {};
- } // namespace Ui
- QT_END_NAMESPACE
- #endif // UI_DMM_H
Please help me in this…..Thanks in advance
25 replies
It seems to me that you’re trying to do a forward declaration of class DMMMenu in the very file where you declare class DMMMenu.
As far as I understand what you want to achieve, you should write:
- namespace DMM {
- namespace Ui {
- {
- Q_OBJECT
- /* ... */
- };
- }
- }
Meaning that you’re defining the class DMMMenu in the DMM::Ui namespace.
hi Johan,
Thanks for your suggestion. You are right exactly I want to do so but upon trying your suggestion (have done before too…:() I got the following error:
/home/pragati/MultiFuncTester/Components/DMM/Build/../Source/DMM.cpp:7: error: ‘DMMMenu’ does not name a type
It is not able to recognize class DMMMenu in DMM.cpp file
:(
help!!!
I think this is because your class is defined in the DMM::Ui namespace, and in your implementation file you are implementing DMM::DMMMenu.
If I were you, I would drop the Ui namespace in the header. Note that I’m unfamiliar with ui files and there might be some black magic in there I don’t know about.
Well, you need the ui file for the private member
- Ui::DMMMenu * ui;
I tried to remove Ui namespace from the header files then I am geeting error…
This is because there’s a naming collision between DMM::DMMMenu and what was Ui::DMMMenu in the following lines:
- namespace DMM {
- ui(new Ui::DMMMenu) // When the Ui namespace is removed, I presume this reads "new DMMMenu".
This is because you’re in the DMM namespace; the name’s resolving to DMM::DMMMenu (which doesn’t have a setupUi function).
If you want to explicitly construct a DMMMenu object, use “new ::DMMMenu”.
It’s because
//# include <../Include/DMM.h> //# include <ui_DMM.h> //What are you trying include? <> <<- means that is known qt path #include "../Include/DMM.h" #include "ui_DMM.h" // ""<<-means that its your path (depends on the path of the project)
Didnt made any difference…still getting same errors
bq. I tried to remove Ui namespace from the header files then I am geeting error…This is because there’s a naming collision between DMM::DMMMenu and what was Ui::DMMMenu in the following lines:
namespace DMM { ui(new Ui::DMMMenu) // When the Ui namespace is removed, I presume this reads "new DMMMenu".This is because you’re in the DMM namespace; the name’s resolving to DMM::DMMMenu (which doesn’t have a setupUi function).
If you want to explicitly construct a DMMMenu object, use “new ::DMMMenu”.
Sorry, I didnt understood….what you want me to change…..
Sorry, I didnt understood….what you want me to change…..
The following assumes that you’ve removed the Ui namespace:-
In DMM.cpp change line 9 from:
- ui(new DMMMenu)
to
- ui(new ::DMMMenu)
I think that should fix your error: /home/pragati/MultiFuncTester/Components/DMM/Build/../Source/DMM.cpp:12: error: ‘class DMM::DMMMenu’ has no member named ‘setupUi’
bq. Sorry, I didnt understood….what you want me to change…..The following assumes that you’ve removed the Ui namespace:-
In DMM.cpp change line 9 from:
ui(new DMMMenu)
to
ui(new ::DMMMenu)I think that should fix your error: /home/pragati/MultiFuncTester/Components/DMM/Build/../Source/DMM.cpp:12: error: ‘class DMM::DMMMenu’ has no member named ‘setupUi’
Still getting the error…..I dont knw what to do…………arggghhhh
Its example.
//mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- namespace Ui {
- class MainWindow;
- }
- {
- Q_OBJECT
- public:
- ~MainWindow();
- private:
- Ui::MainWindow *ui;
- };
- #endif // MAINWINDOW_H
//mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- //use ui: ui->button->setText("Text");
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
————or——————-
//mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include "ui_mainwindow.h"
- {
- Q_OBJECT
- public:
- ~MainWindow();
- private:
- };
- #endif // MAINWINDOW_H
//mainwindow.cpp
- #include "mainwindow.h"
- {
- setupUi(this);
- //use ui: button->setText("Text");
- }
- MainWindow::~MainWindow()
- {
- }
Using now namespace Ui, still getting error:/home/pragati/MultiFuncTester/Components/DMM/Build/../Source/DMM.cpp:14: error: ‘class DMM::Ui::DMMMenu’ has no member named ‘setupUi’
In the example posted by stima_ua, the Ui namespace is being used by the resources framework to generate a Ui::MainWindow class definition. This is the same as in your case – the DMM_ui.h generates a class definition called Ui::DMMMenu – there is nothing called DMM::Ui::DMMMenu defined. However, in your header DMM.h, you have told the compiler that there’s a forward declaration for a type DMM::Ui::DMMMenu – you then use this as the type of your pointer with the lines:
- private:
- Ui::DMMMenu *ui;
Bear in mind – this is telling the compiler: “I want a pointer of type DMM::Ui::DMMMenu” because you’re making this declaration from within the DMM namespace.
If you want to do your implementation using the UI resources from Qt, you’ll need to do your declaration like this:
- namespace Ui {
- class DMMMenu;
- }
- namespace DMM {
- {
- Q_OBJECT
- public:
- ~DMMMenu();
- private:
- ::Ui::DMMMenu *ui;
- };
- }
This tells the compiler that it will receive a full declaration of Ui::DMMMenu later on, and that you want to use Ui::DMMMenu as the type for your ui member pointer in your DMM::DMMMenu class declaration.
You must log in to post a reply. Not a member yet? Register here!


