Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

MOC file header question/problem



  • Hi, I'm new to Qt and I have a few question/problem on MOC file.
    When creating the file moc_test.cpp, it is included test.h files but not any other files needed a moc_test.cpp. Is there a way to automatically include them?
    Now I'll explain with an example

    // Test.h
    
    #include <QtCore>
    
    class AnotherTest;
    
    class Test: public QObject
    {
        Q_OBJECT
    public:
        Test();
    private:
        AnotherTest Atest;   
    }
    
    // Test.cpp
    
    #include "AnotherTest.h"
    #include "Test.h"
    
    Test()
    {
      Atest.Dosomething();
    }
    

    When creating the moc_test.cpp with this configuration, always generates an error because it does not recognize Atest. I would not put AnotherTest.h in Test.h, there is a way to include it automatically in moc_Test.cpp?

    I hope I explained myself.


  • Moderators

    @Gianluca86
    This is not a MOC issue. Your class definition

    class AnotherTest;
    
    class Test: public QObject
    {
        Q_OBJECT
    public:
        Test();
    private:
        AnotherTest Atest;   
    }
    

    is simply not valid C++.
    You cannot forward declare a member variable on the stack. Only members on the heap (pointers).
    Because the member is initialized "immediately" and thus the full class declaration is needed.



  • You're right, I had forgotten it. Thanks so much


Log in to reply