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. Using macros cannot resolve recursive references either.

Using macros cannot resolve recursive references either.

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 279 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.
  • K Offline
    K Offline
    kuqipair
    wrote on last edited by
    #1

    I have three files: classA.h, classB.h, and all_header.h. The details are as follows:

    //classa.h
    #ifndef CLASSA_H
    #define CLASSA_H
    
    #include"all_header.h"
    
    class ClassA
    {
    public:
        ClassA();
        ClassB b1;
    };
    #endif 
    
    //classb.h
    #ifndef CLASSB_H
    #define CLASSB_H
    
    #include"all_header.h"
    
    class ClassB
    {
    public:
        ClassB();
        ClassA* a1;
    };
    #endif // CLASSB_H
    
    
    //all_header.h
    #ifndef ALL_HEADER_H
    #define ALL_HEADER_H
    
    class ClassA;
    class ClassB;
    
    #include"classb.h"
    #include"classa.h"
    
    #endif // ALL_HEADER_H
    
    

    During compilation, the following error occurs:
    error: C2079: 'ClassA::b1' uses undefined class 'ClassB'

    However, when I use the above structure in a standard C++ compiler (whether MSVC or MinGW), it works fine.
    So, why does this happen?

    Pl45m4P jsulmJ JonBJ 3 Replies Last reply
    0
    • K kuqipair

      I have three files: classA.h, classB.h, and all_header.h. The details are as follows:

      //classa.h
      #ifndef CLASSA_H
      #define CLASSA_H
      
      #include"all_header.h"
      
      class ClassA
      {
      public:
          ClassA();
          ClassB b1;
      };
      #endif 
      
      //classb.h
      #ifndef CLASSB_H
      #define CLASSB_H
      
      #include"all_header.h"
      
      class ClassB
      {
      public:
          ClassB();
          ClassA* a1;
      };
      #endif // CLASSB_H
      
      
      //all_header.h
      #ifndef ALL_HEADER_H
      #define ALL_HEADER_H
      
      class ClassA;
      class ClassB;
      
      #include"classb.h"
      #include"classa.h"
      
      #endif // ALL_HEADER_H
      
      

      During compilation, the following error occurs:
      error: C2079: 'ClassA::b1' uses undefined class 'ClassB'

      However, when I use the above structure in a standard C++ compiler (whether MSVC or MinGW), it works fine.
      So, why does this happen?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @kuqipair

      What macros exactly are you talking about?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      K 1 Reply Last reply
      1
      • K kuqipair

        I have three files: classA.h, classB.h, and all_header.h. The details are as follows:

        //classa.h
        #ifndef CLASSA_H
        #define CLASSA_H
        
        #include"all_header.h"
        
        class ClassA
        {
        public:
            ClassA();
            ClassB b1;
        };
        #endif 
        
        //classb.h
        #ifndef CLASSB_H
        #define CLASSB_H
        
        #include"all_header.h"
        
        class ClassB
        {
        public:
            ClassB();
            ClassA* a1;
        };
        #endif // CLASSB_H
        
        
        //all_header.h
        #ifndef ALL_HEADER_H
        #define ALL_HEADER_H
        
        class ClassA;
        class ClassB;
        
        #include"classb.h"
        #include"classa.h"
        
        #endif // ALL_HEADER_H
        
        

        During compilation, the following error occurs:
        error: C2079: 'ClassA::b1' uses undefined class 'ClassB'

        However, when I use the above structure in a standard C++ compiler (whether MSVC or MinGW), it works fine.
        So, why does this happen?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @kuqipair said in Using macros cannot resolve recursive references either.:

        error: C2079: 'ClassA::b1' uses undefined class 'ClassB'

        ClassA::b1 returns ClassB, not ClassB* - this cannot work with forward declaration!

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        K 1 Reply Last reply
        3
        • K kuqipair

          I have three files: classA.h, classB.h, and all_header.h. The details are as follows:

          //classa.h
          #ifndef CLASSA_H
          #define CLASSA_H
          
          #include"all_header.h"
          
          class ClassA
          {
          public:
              ClassA();
              ClassB b1;
          };
          #endif 
          
          //classb.h
          #ifndef CLASSB_H
          #define CLASSB_H
          
          #include"all_header.h"
          
          class ClassB
          {
          public:
              ClassB();
              ClassA* a1;
          };
          #endif // CLASSB_H
          
          
          //all_header.h
          #ifndef ALL_HEADER_H
          #define ALL_HEADER_H
          
          class ClassA;
          class ClassB;
          
          #include"classb.h"
          #include"classa.h"
          
          #endif // ALL_HEADER_H
          
          

          During compilation, the following error occurs:
          error: C2079: 'ClassA::b1' uses undefined class 'ClassB'

          However, when I use the above structure in a standard C++ compiler (whether MSVC or MinGW), it works fine.
          So, why does this happen?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @kuqipair
          In addition to @Pl45m4 [I posted this before @jsulm's response]

          During compilation, the following error occurs:

          However, when I use the above structure in a standard C++ compiler (whether MSVC or MinGW), it works fine.

          What does the first line mean? You must have used a "standard C++ compiler" when you got the error, but then you say that in a "standard C++ compiler" it works fine... ?

          1 Reply Last reply
          1
          • Pl45m4P Pl45m4

            @kuqipair

            What macros exactly are you talking about?

            K Offline
            K Offline
            kuqipair
            wrote on last edited by
            #5

            @Pl45m4 In both classA. h and classB. h, directly referencing the all_ceader. h file achieves the effect of forward declaration. However, when I do this on QT, I encounter compilation errors.

            Pl45m4P 1 Reply Last reply
            0
            • K kuqipair

              @Pl45m4 In both classA. h and classB. h, directly referencing the all_ceader. h file achieves the effect of forward declaration. However, when I do this on QT, I encounter compilation errors.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @kuqipair said in Using macros cannot resolve recursive references either.:

              However, when I do this on QT, I encounter compilation errors.

              There is no "Qt"... You use a "standard C++ compiler" when using Qt in your project...
              Also check as @jsulm pointed out:

              @jsulm said in Using macros cannot resolve recursive references either.:

              lassA::b1 returns ClassB, not ClassB* - this cannot work with forward declaration!


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @kuqipair said in Using macros cannot resolve recursive references either.:

                error: C2079: 'ClassA::b1' uses undefined class 'ClassB'

                ClassA::b1 returns ClassB, not ClassB* - this cannot work with forward declaration!

                K Offline
                K Offline
                kuqipair
                wrote on last edited by
                #7

                @jsulm I implemented this in a QT project. I wanted classA.h and classB.h to directly include the all_header.h file to achieve the effect of forward declarations, but this caused compilation failures. However, when I used the same approach in a regular C++ project compiled with both MSVC and MinGW compilers, it worked successfully.

                JonBJ 1 Reply Last reply
                0
                • K kuqipair

                  @jsulm I implemented this in a QT project. I wanted classA.h and classB.h to directly include the all_header.h file to achieve the effect of forward declarations, but this caused compilation failures. However, when I used the same approach in a regular C++ project compiled with both MSVC and MinGW compilers, it worked successfully.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @kuqipair
                  Then start by comparing the exact compilation lines executed in the Qt and non-Qt case, to see where the difference is.

                  K 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @kuqipair
                    Then start by comparing the exact compilation lines executed in the Qt and non-Qt case, to see where the difference is.

                    K Offline
                    K Offline
                    kuqipair
                    wrote on last edited by
                    #9

                    @JonB Ah-ha, I figured it out!
                    I consulted DeepSeek-R1, and it suggested explicitly defining the precompiled header in the .pro file like this:

                    PRECOMPILED_HEADER = all_header.h
                    CONFIG += precompile_header
                    

                    Big thanks to the amazing open-source AI models from my great motherland (China) - they're truly phenomenal! Highly recommended!

                    JonBJ 1 Reply Last reply
                    0
                    • K kuqipair

                      @JonB Ah-ha, I figured it out!
                      I consulted DeepSeek-R1, and it suggested explicitly defining the precompiled header in the .pro file like this:

                      PRECOMPILED_HEADER = all_header.h
                      CONFIG += precompile_header
                      

                      Big thanks to the amazing open-source AI models from my great motherland (China) - they're truly phenomenal! Highly recommended!

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @kuqipair
                      Had no idea you were using, or wanted to use, precompiled headers.
                      Still doesn't tell me why you would need to: either it's legal C++ or it isn't, don't know why making it precompiled would alter that, but never mind.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        No need for some obscure AI here - simply fix your project structure to avoid recursive includes as this is a big sign for a design flaw. Also use forwarding of classes instead direct includes to reduce compile times.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1
                        • S Offline
                          S Offline
                          SimonSchroeder
                          wrote on last edited by
                          #12

                          Headers alone don't do anything. Suppose you have a classb.cpp that just includes classb.h. This header first declares the macro CLASSB_H. Then you go on to include all_header.h from classb.h. This first includes classb.h followed by classa.h. However, when including classb.h from all_header.h (included from classb.h) CLASSB_H is already defined and the inclusion returns nothing. So far, ClassB has not been defined. Then you go on including classa.h which expects a full definition of ClassB.

                          This problem only occurs when including classb.h (before any of the other two headers). It is not a problem when including classa.h or all_header.h (first). Using all_header.h as a pre-compiled header will include all_header.h before everything else in every translation unit. That's why this solution works. But IMHO this is not a proper solution because source code should always also compile without precompiled headers. Especially in this scenario a change in ClassA or ClassB will trigger a recompile of the precompiled header and thus a recompile of your whole project. This is not a tenable solution for large projects. Precompiled headers should be reserved for third-party libraries that (almost) never change. It is not meant for your own source code. (Precompiled headers are supposed to reduce compilation times and not increase them.)

                          I am not sure what a good solution to this problem is. Most likely it works by removing the include guards for all_header.h. I believe these are (in this specific context) not actually needed.

                          1 Reply Last reply
                          2

                          • Login

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