Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MOC file header question/problem
Forum Updated to NodeBB v4.3 + New Features

MOC file header question/problem

Scheduled Pinned Locked Moved Solved General and Desktop
qt 5.7
3 Posts 2 Posters 694 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Gianluca86G Offline
    Gianluca86G Offline
    Gianluca86
    wrote on last edited by Gianluca86
    #1

    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.

    raven-worxR 1 Reply Last reply
    0
    • Gianluca86G Gianluca86

      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.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Gianluca86G Offline
        Gianluca86G Offline
        Gianluca86
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved