QT_FORWARD_DECLARE_CLASS(ClassName). How?
-
wrote on 13 Oct 2020, 06:32 last edited by
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?
-
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.
-
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.
wrote on 13 Oct 2020, 06:39 last edited by@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.
-
@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.
@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.
-
@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.
-
Lifetime Qt Championwrote on 13 Oct 2020, 06:45 last edited by 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! -
@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???
@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 -
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!wrote on 13 Oct 2020, 06:48 last edited by@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.
-
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. -
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.wrote on 13 Oct 2020, 06:56 last edited by@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.
-
@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 -
@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.
@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?
-
@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.
@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.
-
@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.
1/14