指向用户定义类型的类的全局变量指针

时间:2014-04-24 14:00:41

标签: c++ variables pointers global

我有问题。我想要一个包含我班级地址的全局指针。对于程序的所有其他部分,此指针应该是可见的。这是我的对象源代码:

//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

这是此全局变量指针的头文件

//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 

在标记的行中,我收到编译错误:

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

这是上面使用的类oPsiTestStrategy

//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?

期望的结果应该是这样的:

#include "LP_poGlobalObject.h"

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

谢谢大家,祝你有个美好的一天......: - )

3 个答案:

答案 0 :(得分:2)

您在oPsiTestStrategy中使用

LP_poGlobalObject.h类型时不知道。您必须重新排序包含:

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

您还可以在LP_PSI_Object.h中加入LP_poGlobalObject.h,或在定义该类型的外部对象之前为oPsiTestStrategy做出前瞻声明。

答案 1 :(得分:1)

在头文件中添加 - 在定义全局变量之前...

class oPsiTestStrategy;

所以编译器知道oPsiTestStrategy是什么......

答案 2 :(得分:1)

时必须定义或转发{p> oPsiTestStrategy
extern oPsiTestStrategy* poGlobalObject;
声明

,否则编译器不知道。

添加include指令

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

oPsiTestStrategy类型的转发声明:

class oPsiTestStrategy;