Forward Declaration error



  • 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;

    }

    class DMMMenu: public QWidget
    {
    Q_OBJECT

    public:

    explicit DMMMenu(QWidget *parent = 0);
    ~DMMMenu();

    private:

    Ui::DMMMenu *ui;
    

    };

    }

    #endif // DMM_H
    @

    Here is my DMM.cpp

    @

    include <../Include/DMM.h>

    include <ui_DMM.h>

    namespace DMM {

    DMMMenu::DMMMenu(QWidget *parent) :
    QWidget(parent),
    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:
    QPushButton *pushButton;
    QPushButton *pushButton_2;

    void setupUi(QWidget *DMM)
    {
        if (DMM->objectName().isEmpty())
            DMM->setObjectName(QString::fromUtf8("DMM"));
        DMM->resize(360, 204);
        QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        sizePolicy.setHorizontalStretch(0);
        sizePolicy.setVerticalStretch(0);
        sizePolicy.setHeightForWidth(DMM->sizePolicy().hasHeightForWidth());
        DMM->setSizePolicy(sizePolicy);
        pushButton = new QPushButton(DMM);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setGeometry(QRect(50, 40, 97, 27));
        pushButton_2 = new QPushButton(DMM);
        pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
        pushButton_2->setGeometry(QRect(190, 40, 97, 27));
    
        retranslateUi(DMM);
    
        QMetaObject::connectSlotsByName(DMM);
    } // setupUi
    
    void retranslateUi(QWidget *DMM)
    {
        DMM->setWindowTitle(QApplication::translate("DMM", "Form", 0, QApplication::UnicodeUTF8));
        pushButton->setText(QApplication::translate("DMM", "PushButton", 0, QApplication::UnicodeUTF8));
        pushButton_2->setText(QApplication::translate("DMM", "PushButton", 0, QApplication::UnicodeUTF8));
    } // 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



  • 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 {
    
        class DMMMenu: public QWidget
        {
            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.



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



  • Well, you need the ui file for the private member 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.



  • 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'



  • 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)
    @



  • 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 {

    DMMMenu::DMMMenu(QWidget *parent) :
    QWidget(parent),
    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".



  • [quote author="stima_ua" date="1337331690"]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)
    @
    [/quote]

    Didnt made any difference...still getting same errors



  • [quote author="r3willia" date="1337332133"]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 {

    DMMMenu::DMMMenu(QWidget *parent) :
    QWidget(parent),
    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".[/quote]

    Sorry, I didnt understood....what you want me to change.....



  • 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’



  • [quote author="r3willia" date="1337333494"]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’[/quote]

    Still getting the error.....I dont knw what to do............arggghhhh



  • 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'



  • Its example.

    //mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    //mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    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"

    class MainWindow : public QMainWindow, public Ui_MainWindow
    {
    Q_OBJECT

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

    private:

    };

    #endif // MAINWINDOW_H
    @

    //mainwindow.cpp
    @
    #include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
    setupUi(this);

    //use ui: button->setText("Text");
    

    }

    MainWindow::~MainWindow()
    {
    }
    @



  • [quote author="pragati" date="1337334158"] 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'[/quote]

    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 {

    class DMMMenu: public QWidget
    {
    Q_OBJECT

    public:

    explicit DMMMenu(QWidget *parent = 0);
    ~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.



  • I did as you guys told

    @
    namespace Ui {

    class DMMMenu;

    }

    namespace DMM {

    class DMMMenu: public QWidget
    {
    Q_OBJECT

    public:

    explicit DMMMenu(QWidget *parent = 0);
    ~DMMMenu();

    private:

    ::Ui::DMMMenu *ui;
    

    };

    }@

    But now the compiler is telling the prototype in dmm.cpp and dmm_ui.h doesnot match



  • I think that's progress. What's the exact message you're seeing?



  • :).....

    1 /home/pragati/MultiFuncTester/Components/DMM/Build/../Source/DMM.cpp:8: error: prototype for 'Ui::DMMMenu::DMMMenu(QWidget*)' does not match any in class 'Ui::DMMMenu'

    1. /home/pragati/MultiFuncTester/Components/DMM/Build/ui_DMM.h:61: error: candidates are: Ui::DMMMenu::DMMMenu(const Ui::DMMMenu&)

    error: Ui::DMMMenu::DMMMenu()



  • From the code you've posted of DMM_ui.h, Ui::DMMMenu has no explicit constructor - this means the compiler will have given it an implicit copy constructor of signature Ui::DMMMenu::DMMMenu(const Ui::DMMMenu&) This is the candidate you're seeing.

    Do you still use the Ui namespace in your DMM.cpp file? It looks like the compiler's trying to link DMM::DMMMenu against Ui::DMMMenu!

    Make sure if there's any ambiguity that you remove it by explicitly specifying the name of the type you're using. Is your code in DMM.cpp now looking something like this?
    @
    # include <../Include/DMM.h>
    # include <ui_DMM.h>

    namespace DMM {
     
    DMMMenu::DMMMenu(QWidget *parent) :
        QWidget(parent),
        ui(new ::Ui::DMMMenu)
    {
        ui->setupUi(this);
    }
     
    }@


  • I did changed to Namespace DMM...but prob is I have to create an instance of this DMM in other directory class like this:
    Start.h
    @
    #ifndef START_H
    #define START_H

    #include "../../DMM/Include/DMM.h"

    /**************************************************************************/
    /

    • \brief This class handles the initialization of the user interface.
      /
      /
      ***************************************************************************/
      namespace Start {

    class Startup : public QObject
    {
    Q_OBJECT

    public:
    Startup();
    virtual ~Startup();
    void GuiInit();

    private:

    Ui::DMMMenu *MainWindow;

    };

    }

    #endif // START_H
    @

    Start.cpp:

    @

    #include "../../Start/Include/Start.h"

    namespace Start {

    Startup::Startup() : QObject()
    {
    
    // GUI components
    
    MainWindow = new DMM::DMMMenu();
    

    }

    Startup::~Startup()
    {
    try {
    // GUI Components

        delete MainWindow;
    }
    catch (...) {}
    

    }

    /***************************************************************************/
    /
    !

    • \brief Initializes the user interface
      /
      /
      ***************************************************************************/
      void Startup::GuiInit()
      {
      MainWindow->show();

    }

    }

    @

    So now I am getting errors like these upon changing namespace to DMM:

    1. /home/pragati/MultiFuncTester/Components/Start/Build/../Source/Start.cpp:-1: In constructor 'Start::Startup::Startup()':

    /home/pragati/MultiFuncTester/Components/Start/Build/../Source/Start.cpp:12: error: cannot convert 'DMM::DMMMenu' to 'Ui::DMMMenu*' in assignment*

    2./home/pragati/MultiFuncTester/Components/Start/Build/../Source/Start.cpp:35: error: invalid use of incomplete type 'struct Ui::DMMMenu'

    3./home/pragati/MultiFuncTester/Components/Start/Build/../../DMM/Include/DMM.h:9: error: forward declaration of 'struct Ui::DMMMenu'



  • That's good - it means the compiler's got past the problem you were having before - now you've got the problem that you're using the wrong type for your member of Start::Startup.

    I expect you want Start::Startup's declaration to read:

    @class Startup : public QObject
    {
    Q_OBJECT

    public:
    Startup();
    virtual ~Startup();
    void GuiInit();

    private:

    DMM::DMMMenu *MainWindow;

    };@

    DMM::DMMMenu has a Ui::DMMMenu, but DMM::DMMMenu is not a Ui::DMMMenu (to use Object-orientated is-a / has-a terminology).



  • :(

    I used it already before but as soon as I do this:
    @DMM::DMMMenu *MainWindow;
    @
    I get error that
    1./home/pragati/MultiFuncTester/Components/Start/Build/../../Start/Include/Start.h:27: error: 'DMM' does not name a type

    and

    2./home/pragati/MultiFuncTester/Components/Start/Build/../Source/Start.cpp:35: error: 'MainWindow' was not declared in this scope



  • You're referencing DMM from within the namespace Start; it's possible you may have to be specific i.e. declare the parameter:
    @::DMM::DMMMenu *MainWindow;@

    Also make certain that "../../DMM/Include/DMM.h" does indeed define DMM::DMMMenu.



  • r3Willia,

    I am still getting errors but not regarding this....anyways thank you so much...you came when I was not getting help from anyone....Thank you so much....wish I cud have personally thanked you...bt anyways....all the best and again ty....



  • hi...

    Everything is working fine until when I write in main. In main I included one of the subdirectory's .h file and wrote some function like this I am posting here both the codes of main and that directory....

    Error I am getting is :

    /home/pragati/MultiFuncTester/Components/Main/Source/Main.cpp:-1: error: undefined reference to `Start::Startup::Startup()'

    /home/pragati/MultiFuncTester/Components/Main/Source/Main.cpp:-1: error: undefined reference to `Start::Startup::GuiInit()'
    /home/pragati/MultiFuncTester/Components/Main/Source

    /Main.cpp:-1: error: undefined reference to `Start::Startup::~Startup()'

    Similiar error for GuiInit....

    Start.h

    @
    #ifndef START_H
    #define START_H

    #include "../../DMM/Include/DMM.h"

    /**************************************************************************/
    /

    • \brief This class handles the initialization of the user interface.
      /
      /
      ***************************************************************************/
      namespace Start {

    class Startup : public QObject
    {
    Q_OBJECT

    public:

    Startup();
    virtual ~Startup();
    void GuiInit();

    private:

    DMM::DMMMenu *MainWindow;

    };

    }

    #endif // START_H

    @

    Start.cpp

    @

    #include "../../Start/Include/Start.h"
    namespace Start {

    Startup::Startup() : QObject()
    {

    // // GUI components

    MainWindow = new DMM::DMMMenu();

    }

    /***************************************************************************/
    /
    !

    • \brief Initializes the user interface
      /
      /
      ***************************************************************************/
      void Startup::GuiInit()
      {
      MainWindow->show();

    }

    }
    @

    Main.cpp

    @
    #include "../../Start/Include/Start.h"
    #include <QApplication>
    #include <QtGui>

    int main(int argc, char *argv[])

    {
    QApplication app(argc, argv);
    Start::Startup start;
    start.GuiInit();
    return app.exec();
    }

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.