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. QT_FORWARD_DECLARE_CLASS(ClassName). How?

QT_FORWARD_DECLARE_CLASS(ClassName). How?

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 2.9k 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by
    #1

    Hello all!

    Got stacked on simple issue. Totally forgotten how to use QT_FORWARD_DECLARE_CLASS(ClassName). For now I have this error when I am trying to call method from from forwarded class:

    ../ForwardDeclaration_v1/aobjecta.cpp:57:10: error: member access into incomplete type 'ABackend'
            pBackend->mTestMethod("Inside AObjectA");
                    ^
    ../ForwardDeclaration_v1/aobjecta.h:30:26: note: forward declaration of 'ABackend'
    QT_FORWARD_DECLARE_CLASS(ABackend)
                             ^
    

    The class is very simple.
    Class *.h:

    // System includes
    #include <QObject>
    
    // Application includes
    #include "alogger.h"
    
    // Constants and definitions
    
    // Namespace
    
    // Classes
    QT_FORWARD_DECLARE_CLASS(ABackend)
    //class ABackend;
    
    // Class definitions
    class AObjectA : public QObject {
    
    	Q_OBJECT
    
    	public:
    
    		ABackend* pBackend = nullptr;
    
    		explicit AObjectA(ABackend* inBackend, QObject* parent = nullptr);
    		virtual ~AObjectA(void);
    
    		void mTestMethod(void);
    };
    

    Class *.cpp:

    // Class header
    #include "aobjecta.h"
    
    
    // -----------
    /*!
    	\fn
    
    	Doc.
    */
    
    AObjectA::AObjectA(ABackend* inBackend, QObject *parent) : QObject(parent) {
    
    	pBackend = inBackend;
    
    	ALOG_SYSTEM << "AObjectA created";
    }
    
    
    // -----------
    /*!
    	\fn
    
    	Doc.
    */
    
    AObjectA::~AObjectA(void) {
    
    	ALOG_SYSTEM << "AObjectA created";
    }
    
    
    // -----------
    /*!
    	\fn
    
    	Doc.
    */
    
    void AObjectA::mTestMethod(void) {
    
    	pBackend->mTestMethod("Inside AObjectA"); // The place of error
    }
    
    

    What am I missing?

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

      C++ basics - when you want to use a class, the definition of the class must be visible to the compiler. Therefore you have to include it's header.

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

      B 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        C++ basics - when you want to use a class, the definition of the class must be visible to the compiler. Therefore you have to include it's header.

        B Offline
        B Offline
        bogong
        wrote on last edited by
        #3

        @Christian-Ehrlicher How it related to be using QT_FORWARD_DECLARE_CLASS(ClassName)??? Because When I am using it in pure everything working fine. The question isn't about SIMPLE CPP but about QT_FORWARD_DECLARE_CLASS(ClassName) macros. Here the published example of pure cpp I know perfectly about SIMPLE CPP. I am trying to solve issue with macros.

        jsulmJ 1 Reply Last reply
        0
        • B bogong

          @Christian-Ehrlicher How it related to be using QT_FORWARD_DECLARE_CLASS(ClassName)??? Because When I am using it in pure everything working fine. The question isn't about SIMPLE CPP but about QT_FORWARD_DECLARE_CLASS(ClassName) macros. Here the published example of pure cpp I know perfectly about SIMPLE CPP. I am trying to solve issue with macros.

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

          @bogong said in QT_FORWARD_DECLARE_CLASS(ClassName). How?:

          QT_FORWARD_DECLARE_CLASS(ClassName)

          As far as I know this is exactly the same as writing

          class ClassName;
          

          So, you have to include the header file in cpp file.

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

          B 1 Reply Last reply
          1
          • jsulmJ jsulm

            @bogong said in QT_FORWARD_DECLARE_CLASS(ClassName). How?:

            QT_FORWARD_DECLARE_CLASS(ClassName)

            As far as I know this is exactly the same as writing

            class ClassName;
            

            So, you have to include the header file in cpp file.

            B Offline
            B Offline
            bogong
            wrote on last edited by
            #5

            @jsulm Yes you right. But when I am using inherited from QObject - something happening. When I am using on pure CPP - everything works fine. Is there something that I need to declare in forwarded class???

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

              First: there is no need to use this macro (see https://bugreports.qt.io/browse/QTBUG-37137 - "Nco one but Qt developers themselves should need those macros.") - it's not even documented.
              Second: your problem is a basic c++ problem - you have to include the header of a class when you want to use it!

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

              B 1 Reply Last reply
              2
              • B bogong

                @jsulm Yes you right. But when I am using inherited from QObject - something happening. When I am using on pure CPP - everything works fine. Is there something that I need to declare in forwarded class???

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

                @bogong No, you just need to include the header file containing the class in CPP file (and maybe use the correct namespace).
                The only think this macro does in addition to forward declaration is putting it in the namespace, see https://stackoverflow.com/questions/45118284/pre-declaration-and-the-inclusion-of-header-files-bese-on-qt

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

                B 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  First: there is no need to use this macro (see https://bugreports.qt.io/browse/QTBUG-37137 - "Nco one but Qt developers themselves should need those macros.") - it's not even documented.
                  Second: your problem is a basic c++ problem - you have to include the header of a class when you want to use it!

                  B Offline
                  B Offline
                  bogong
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher I've been including it through another header file and it's not working like in pure CPP. When I am using it directly in CPP all is working.

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

                    So you want me to say that alogger.h includes ABackend definition? Why do you need a forward decl then?
                    You don't include other headers.

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

                    B 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      So you want me to say that alogger.h includes ABackend definition? Why do you need a forward decl then?
                      You don't include other headers.

                      B Offline
                      B Offline
                      bogong
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher When I do include it into class header directly it's rising new error with incomplete but pointing on another Class. At the first it's pointing error onto forwarded class when I am using direct implementation it's pointing on class that is using forwarded. I've tried to avoid using header directly in *.cpp files. That is my mistake. Usually I am using one header file in one cpp and all inclusions through header.

                      Christian EhrlicherC jsulmJ 2 Replies Last reply
                      0
                      • jsulmJ jsulm

                        @bogong No, you just need to include the header file containing the class in CPP file (and maybe use the correct namespace).
                        The only think this macro does in addition to forward declaration is putting it in the namespace, see https://stackoverflow.com/questions/45118284/pre-declaration-and-the-inclusion-of-header-files-bese-on-qt

                        B Offline
                        B Offline
                        bogong
                        wrote on last edited by
                        #11

                        @jsulm Thx, your link helped. Issue closed. Solution found.

                        1 Reply Last reply
                        0
                        • B bogong

                          @Christian-Ehrlicher When I do include it into class header directly it's rising new error with incomplete but pointing on another Class. At the first it's pointing error onto forwarded class when I am using direct implementation it's pointing on class that is using forwarded. I've tried to avoid using header directly in *.cpp files. That is my mistake. Usually I am using one header file in one cpp and all inclusions through header.

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @bogong said in QT_FORWARD_DECLARE_CLASS(ClassName). How?:

                          I've tried to avoid using header directly in *.cpp files.

                          How should this work at all?

                          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
                          • B bogong

                            @Christian-Ehrlicher When I do include it into class header directly it's rising new error with incomplete but pointing on another Class. At the first it's pointing error onto forwarded class when I am using direct implementation it's pointing on class that is using forwarded. I've tried to avoid using header directly in *.cpp files. That is my mistake. Usually I am using one header file in one cpp and all inclusions through header.

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

                            @bogong said in QT_FORWARD_DECLARE_CLASS(ClassName). How?:

                            Usually I am using one header file in one cpp and all inclusions through header.

                            Not a good idea. Better to use forward declarations in header file if possible and include their header files in cpp file. Can improve compile time for example and keeps your header file smaller after precompiler run.

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

                            B 1 Reply Last reply
                            4
                            • jsulmJ jsulm

                              @bogong said in QT_FORWARD_DECLARE_CLASS(ClassName). How?:

                              Usually I am using one header file in one cpp and all inclusions through header.

                              Not a good idea. Better to use forward declarations in header file if possible and include their header files in cpp file. Can improve compile time for example and keeps your header file smaller after precompiler run.

                              B Offline
                              B Offline
                              bogong
                              wrote on last edited by
                              #14

                              @jsulm Got it, now. Thx. This is what I've got forgotten.

                              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