Zdeno Pavlik
Zdeno Pavlik

Reputation: 758

Global variable pointer to class of user-defined type

I have problem. I want to have one global pointer containing adress of my class. And this pointer should be visible for all other parts of program. This is my source code of objects:

//FILEname:LP_poGlobalObject.cpp    

#include "LP_poGlobalObject.h"  //this is header file to be included by all other files
#include "LP_PSI_Object.h"      //include oPsiTestStrategy class

    oPsiTestStrategy* poGlobalObject = NULL;  
                     //create new pointer supposed to be global

this is header file for this global variable pointer

//FILEname:LP_poGlobalObject.h

#if !defined LP_PSI_OBJECT_GLOBAL_H
#define LP_PSI_OBJECT_GLOBAL_H

    extern oPsiTestStrategy* poGlobalObject; //error C2143: syntax error : missing '{' before '*'

#endif 

At the marked line I get a compiler error:

error C2143: syntax error : missing '{' before '*'

and this is class oPsiTestStrategy used above

//FILEname:LP_PSI_Object.h

#if !defined LP_PSI_OBJECT_H 
#define LP_PSI_OBJECT_H

class oPsiTestStrategy
        {
           public:
                oPsiTestStrategy();
                virtual ~oPsiTestStrategy();
                virtual Word    zPsiGetSourceId_Testing_O(EvtId xEvtPktId);
                virtual Dword   zPsiGetData_Testing_O(EvtId, void*);
                virtual Dword   zPsiGetDataLen_Testing_O( EvtId );
                virtual void    vPsiExitEvent_Testing_O( EvtId );
                virtual void    vPsiCancelEvent_Testing_O( EvtId );
                virtual void    vPsiSetUserStatus_Testing_O( EvtId , Word );
        }; 
#endif
// methods are virtual because I want to test this and replace it with mock objects - GTest, UnitTesting. 
//Is this causing my problem?

Desired result of this should be something like this:

#include "LP_poGlobalObject.h"

int main()
{
poGlobalObject->vPsiCancelEvent_Testing_O((EvtId)5);
}

Thanks people, have a nice day.. :-)

Upvotes: 1

Views: 1990

Answers (3)

4pie0
4pie0

Reputation: 29724

oPsiTestStrategy has to be defined or forward declared when

extern oPsiTestStrategy* poGlobalObject;

is declared, otherwise it is not known to the compiler.

Add include directive

#include "LP_poGlobalObject.h"
#include "LP_PSI_Object.h" 

or forward declaration for oPsiTestStrategy type:

class oPsiTestStrategy;

Upvotes: 1

Anonymouse
Anonymouse

Reputation: 945

In your header file add - before definition of global variable...

class oPsiTestStrategy;

So the compiler knows what a oPsiTestStrategy is...

Upvotes: 1

Marius Bancila
Marius Bancila

Reputation: 16318

oPsiTestStrategy type is not know at the point where you use it in LP_poGlobalObject.h. You have to reorder the includes:

#include "LP_PSI_Object.h"      //include oPsiTestStrategy class
#include "LP_poGlobalObject.h"  //this is header file to be included by all other files

You could also include LP_PSI_Object.h in LP_poGlobalObject.h or make a forward declaration for oPsiTestStrategy before defining an external object of that type.

Upvotes: 2

Related Questions