簡體   English   中英

指向用戶定義類型的類的全局變量指針

[英]Global variable pointer to class of user-defined type

我有問題。 我想要一個包含我班級地址的全局指針。 並且該指針對於程序的所有其他部分都應該可見。 這是我的對象的源代碼:

//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);
}

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

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_poGlobalObject.h包含LP_PSI_Object.h或對oPsiTestStrategy前向聲明, oPsiTestStrategy再定義該類型的外部對象。

在頭文件中添加-在定義全局變量之前...

class oPsiTestStrategy;

因此,編譯器知道什么是oPsiTestStrategy ...

oPsiTestStrategy必須在以下情況定義或轉發聲明

extern oPsiTestStrategy* poGlobalObject;

聲明,否則編譯器不知道。

添加包含指令

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

或轉發聲明oPsiTestStrategy類型:

class oPsiTestStrategy;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM