May 18, 2012

pragati pragati
Ant Farmer
116 posts

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

  1. #ifndef DMM_H
  2. #define DMM_H
  3. #include <QtGui>
  4. #include <QWidget>
  5.  
  6.  
  7. namespace DMM {
  8.  
  9. namespace Ui {
  10.  
  11. class DMMMenu;
  12.  
  13. }
  14.  
  15. class DMMMenu: public QWidget
  16. {
  17.     Q_OBJECT
  18.  
  19. public:
  20.  
  21.  
  22. explicit DMMMenu(QWidget *parent = 0);
  23.     ~DMMMenu();
  24.  
  25.  
  26.  
  27. private:
  28.  
  29.  
  30.     Ui::DMMMenu *ui;
  31. };
  32.  
  33. }
  34.  
  35. #endif // DMM_H

Here is my DMM.cpp

  1. # include <../Include/DMM.h>
  2. # include <ui_DMM.h>
  3.  
  4.  
  5. namespace DMM {
  6.  
  7. DMMMenu::DMMMenu(QWidget *parent) :
  8.     QWidget(parent),
  9.     ui(new Ui::DMMMenu)
  10. {
  11.     ui->setupUi(this);
  12. }
  13.  
  14. }

And this is the DMM_ui.h

  1. /********************************************************************************
  2. ** Form generated from reading UI file 'DMM.ui'
  3. **
  4. ** Created: Fri May 18 10:47:28 2012
  5. **      by: Qt User Interface Compiler version 4.8.0
  6. **
  7. ** WARNING! All changes made in this file will be lost when recompiling UI file!
  8. ********************************************************************************/
  9.  
  10. #ifndef UI_DMM_H
  11. #define UI_DMM_H
  12.  
  13. #include <QtCore/QVariant>
  14. #include <QtGui/QAction>
  15. #include <QtGui/QApplication>
  16. #include <QtGui/QButtonGroup>
  17. #include <QtGui/QHeaderView>
  18. #include <QtGui/QPushButton>
  19. #include <QtGui/QWidget>
  20.  
  21. QT_BEGIN_NAMESPACE
  22.  
  23. class Ui_DMM
  24. {
  25. public:
  26.     QPushButton *pushButton;
  27.     QPushButton *pushButton_2;
  28.  
  29.     void setupUi(QWidget *DMM)
  30.     {
  31.         if (DMM->objectName().isEmpty())
  32.             DMM->setObjectName(QString::fromUtf8("DMM"));
  33.         DMM->resize(360, 204);
  34.         QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  35.         sizePolicy.setHorizontalStretch(0);
  36.         sizePolicy.setVerticalStretch(0);
  37.         sizePolicy.setHeightForWidth(DMM->sizePolicy().hasHeightForWidth());
  38.         DMM->setSizePolicy(sizePolicy);
  39.         pushButton = new QPushButton(DMM);
  40.         pushButton->setObjectName(QString::fromUtf8("pushButton"));
  41.         pushButton->setGeometry(QRect(50, 40, 97, 27));
  42.         pushButton_2 = new QPushButton(DMM);
  43.         pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
  44.         pushButton_2->setGeometry(QRect(190, 40, 97, 27));
  45.  
  46.         retranslateUi(DMM);
  47.  
  48.         QMetaObject::connectSlotsByName(DMM);
  49.     } // setupUi
  50.  
  51.     void retranslateUi(QWidget *DMM)
  52.     {
  53.         DMM->setWindowTitle(QApplication::translate("DMM", "Form", 0, QApplication::UnicodeUTF8));
  54.         pushButton->setText(QApplication::translate("DMM", "PushButton", 0, QApplication::UnicodeUTF8));
  55.         pushButton_2->setText(QApplication::translate("DMM", "PushButton", 0, QApplication::UnicodeUTF8));
  56.     } // retranslateUi
  57.  
  58. };
  59.  
  60. namespace Ui {
  61.     class DMMMenu: public Ui_DMM {};
  62. } // namespace Ui
  63.  
  64. QT_END_NAMESPACE
  65.  
  66. #endif // UI_DMM_H

Please help me in this…..Thanks in advance

25 replies

May 18, 2012

Johan Solo Johan Solo
Lab Rat
90 posts

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:

  1. namespace DMM {
  2.  
  3.     namespace Ui {
  4.  
  5.         class DMMMenu: public QWidget
  6.         {
  7.             Q_OBJECT
  8.             /* ... */
  9.         };
  10.     }
  11. }

Meaning that you’re defining the class DMMMenu in the DMM::Ui namespace.

 Signature 

Linux : you can find worse, but it’s more expensive.

May 18, 2012

pragati pragati
Ant Farmer
116 posts

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!!!

May 18, 2012

Johan Solo Johan Solo
Lab Rat
90 posts

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.

 Signature 

Linux : you can find worse, but it’s more expensive.

May 18, 2012

pragati pragati
Ant Farmer
116 posts

lolzzz…..

I know the error but if I dont use Ui namespace how wud I use the Dmm_ui.h file….I really dnt have any idea what to remove…..Hate namespace!!!!!

May 18, 2012

Johan Solo Johan Solo
Lab Rat
90 posts

Well, you need the ui file for the private member

  1. Ui::DMMMenu * ui;
which already mentions the Ui namespace… and Ui::DMMMenu is just an alias to the Ui_DMM class defined in the ui file. The more I think about this the more I tend to believe you don’t need to use Ui namespace in your header as long as the ui member has type Ui::DMMMenu.

 Signature 

Linux : you can find worse, but it’s more expensive.

May 18, 2012

pragati pragati
Ant Farmer
116 posts

I tried to remove Ui namespace from the header files then I am geeting error :

/home/pragati/MultiFuncTester/Components/DMM/Build/../Source/DMM.cpp:12: error: ‘class DMM::DMMMenu’ has no member named ‘setupUi’

May 18, 2012

stima_ua stima_ua
Lab Rat
65 posts

It’s because

  1. //# include <../Include/DMM.h>
  2. //# include <ui_DMM.h> //What are you trying include? <> <<- means that is known qt path
  3. #include "../Include/DMM.h"
  4. #include "ui_DMM.h" // ""<<-means that its your path (depends on the path of the project)

May 18, 2012

r3willia r3willia
Lab Rat
8 posts

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:

  1. namespace DMM {
  2.  
  3. DMMMenu::DMMMenu(QWidget *parent) :
  4.     QWidget(parent),
  5.     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”.

May 18, 2012

pragati pragati
Ant Farmer
116 posts
stima_ua wrote:
It’s because
  1. //# include <../Include/DMM.h>
  2. //# include <ui_DMM.h> //What are you trying include? <> <<- means that is known qt path
  3. #include "../Include/DMM.h"
  4. #include "ui_DMM.h" // ""<<-means that its your path (depends on the path of the project)

Didnt made any difference…still getting same errors

May 18, 2012

pragati pragati
Ant Farmer
116 posts

r3willia wrote:
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:

  1. namespace DMM {
  2.  
  3. DMMMenu::DMMMenu(QWidget *parent) :
  4.     QWidget(parent),
  5.     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…..

May 18, 2012

r3willia r3willia
Lab Rat
8 posts

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:

  1.     ui(new DMMMenu)

to
  1.     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’

May 18, 2012

pragati pragati
Ant Farmer
116 posts

r3willia wrote:
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:

  1.     ui(new DMMMenu)

to
  1.     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

May 18, 2012

pragati pragati
Ant Farmer
116 posts
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’

May 18, 2012

stima_ua stima_ua
Lab Rat
65 posts

Its example.

//mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5.  
  6. namespace Ui {
  7. class MainWindow;
  8. }
  9.  
  10. class MainWindow : public QMainWindow
  11. {
  12.     Q_OBJECT
  13.    
  14. public:
  15.     explicit MainWindow(QWidget *parent = 0);
  16.     ~MainWindow();
  17.    
  18. private:
  19.     Ui::MainWindow *ui;
  20. };
  21.  
  22. #endif // MAINWINDOW_H

//mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5.     QMainWindow(parent),
  6.     ui(new Ui::MainWindow)
  7. {
  8.     ui->setupUi(this);
  9.  
  10.     //use ui: ui->button->setText("Text");
  11. }
  12.  
  13. MainWindow::~MainWindow()
  14. {
  15.     delete ui;
  16. }

————or——————-
//mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include "ui_mainwindow.h"
  6.  
  7. class MainWindow : public QMainWindow, public Ui_MainWindow
  8. {
  9.     Q_OBJECT
  10.    
  11. public:
  12.     explicit MainWindow(QWidget *parent = 0);
  13.     ~MainWindow();
  14.  
  15. private:
  16.  
  17. };
  18.  
  19. #endif // MAINWINDOW_H

//mainwindow.cpp

  1. #include "mainwindow.h"
  2.  
  3. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  4. {
  5.     setupUi(this);
  6.  
  7.     //use ui: button->setText("Text");
  8. }
  9.  
  10. MainWindow::~MainWindow()
  11. {
  12. }

May 18, 2012

r3willia r3willia
Lab Rat
8 posts

pragati wrote:
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:

  1. private:
  2.  
  3.  
  4.     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:

  1. namespace Ui {
  2.  
  3. class DMMMenu;
  4.  
  5. }
  6.  
  7. namespace DMM {
  8.  
  9. class DMMMenu: public QWidget
  10. {
  11.     Q_OBJECT
  12.  
  13. public:
  14.  
  15.  
  16. explicit DMMMenu(QWidget *parent = 0);
  17.     ~DMMMenu();
  18.  
  19.  
  20.  
  21. private:
  22.  
  23.  
  24.     ::Ui::DMMMenu *ui;
  25. };
  26.  
  27. }

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.

Page  
1

  ‹‹ How to read MP3 Tags Using QT      QVector<QRect> QRegion::rects() const : what’s behind? ››

You must log in to post a reply. Not a member yet? Register here!