错误 LNK2019 - 抽象类中的虚拟析构函数
可能的重复:
C++ 中的纯虚拟析构函数
我有两个类:抽象的“Game”类和派生“TestGame”类。 TestGame 中的所有函数都是单独实现的(为了编译)。我只收到一个错误:
TestGame.obj:错误 LNK2019: 未解析的外部符号“public: 虚拟 __thiscall 游戏::~Game(void)" (??1Game@@UAE@XZ) 中引用 函数“公共:虚拟__thiscall 测试游戏::~测试游戏(无效)” (??1TestGame@@UAE@XZ)
这是我的类定义:
class Game
{
public:
virtual ~Game(void) = 0;
virtual bool Initialize() = 0;
virtual bool LoadContent() = 0;
virtual void Update() = 0;
virtual void Draw() = 0;
};
class TestGame: public Game
{
public:
TestGame(void);
virtual ~TestGame(void);
virtual bool Initialize();
virtual bool LoadContent();
virtual void Update();
virtual void Draw();
};
我已经尝试了一些方法,但我觉得我可能缺少一些关于抽象和派生类如何工作的基本知识。
Possible Duplicate:
Pure virtual destructor in C++
I have two classes: the abstract "Game" class and the derived "TestGame" class. All of the functions in TestGame are implemented separately to nothing (for the sake of getting it to compile). I am only getting one error:
TestGame.obj : error LNK2019:
unresolved external symbol "public:
virtual __thiscall Game::~Game(void)"
(??1Game@@UAE@XZ) referenced in
function "public: virtual __thiscall
TestGame::~TestGame(void)"
(??1TestGame@@UAE@XZ)
Here are my class definitions:
class Game
{
public:
virtual ~Game(void) = 0;
virtual bool Initialize() = 0;
virtual bool LoadContent() = 0;
virtual void Update() = 0;
virtual void Draw() = 0;
};
class TestGame: public Game
{
public:
TestGame(void);
virtual ~TestGame(void);
virtual bool Initialize();
virtual bool LoadContent();
virtual void Update();
virtual void Draw();
};
I've tried a couple of things but I feel that maybe I am missing something fundamental about how abstracting and deriving classes works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您需要为基类定义析构函数,即使它是纯虚拟的,因为当派生类被销毁时它将被调用。
纯虚函数的
= 0
不是必需的,因为您还有其他纯虚函数来使您的类抽象。You actually need to define the destructor for the base class even though it is pure virtual, since it will be called when the derived class is destroyed.
The
= 0
for pure virtual is not necessary, since you have other pure virtual functions to make your class abstract.