我一直在想办法解决这个问题。我想从带有静态指针的类继承,但我得到错误LNK2001:无法解析的外部符号“ protected :静态类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

最佳答案

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

##include "game.h"

//do this .cpp file

cGame* cEvent::mGame = nullptr;

//or initialize it as : cGame* cEvent::mGame = create object!

请注意,类中的静态成员仅是声明,而不是定义。

10-08 20:01