PDA

View Full Version : Forward Class declaration ERROR



nleverin
30th July 2007, 04:57
Hi,

Within my application class header i have implemented global forward class declaration as follows:

#include bla

class Prep;
Prep* pPrep;

class Prep : public QMainWindow
{

....

};

When compiling im getting to following error messages:
.obj/main.o/user/lib/qt3/include/qglist.h first defined here line 150
.obj/navTopDlg.o:/usr/lib/qt3/include/qglist.h multiple definition of pPrep line 150
/usr/lib/qt3/include/qglist.h multiple definitions of pntPrep line 150

was just wondering if anyone knew anything about this problem with qglist when making forward class declarations.

marcel
30th July 2007, 08:35
You can't instantiate a forward declared class until the class definition for that class has been encountered by the linker.

Try declaring it with extern. Should work. Basically you are telling the linker that this class is defined in some object file, forcing external linkage. You are just using it here.

But, the bottom line is that instantiating objects in headers is not a good practice.
Do you really need that object in all the cpp files in which the header will be included? It is hard to believe.
There is a programming pattern, called Singleton. I think this best suites your needs if you need a single instance of an object across an application.
You just create a class with a private constructor and a getter function which returns the single instance of this class.
You can find more about it on the internet.

Regards