指向类链接器错误的静态指针

PSi*_*ilo 1 c++ static linker-errors

我一直在试图寻找一个解决方案.我想继承一个带有静态指针的类,但我是geterror LNK2001:未解析的外部符号"protected:static class cGame*cEvent :: mGame"(?mGame @ cEvent @@ 1PAVcGame @@ A)

最后我会初始化我的类cEvent一次,然后不要在继承的类中传递指针.

#ifndef EVENT_H
#define EVENT_H

#include "game.h"


class cEvent
{
protected:
    static cGame* mGame;
public:
    cEvent(){;}
    virtual void doEvent(){;}
};

class cEventExitButton: public cEvent
{
private:
public:
    cEventExitButton(cGame *g){mGame = g;}
    void doEvent(){mGame->getWindow()->close();}
};

#endif
Run Code Online (Sandbox Code Playgroud)

Naw*_*waz 5

您需要定义的static成员外的类:

##include "game.h"

//do this .cpp file

cGame* cEvent::mGame = nullptr;

//or initialize it as : cGame* cEvent::mGame = create object!
Run Code Online (Sandbox Code Playgroud)

请注意,类中的静态成员只是声明,即不是定义.