错误LNK2019-抽象类中的虚拟析构函数 [英] error LNK2019 - Virtual destructor in abstract class

查看:182
本文介绍了错误LNK2019-抽象类中的虚拟析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
C ++中的纯虚拟析构器

Possible Duplicate:
Pure virtual destructor in C++

我有两个类:抽象的"Game"类和派生的"TestGame"类. TestGame中的所有功能都是单独实现的(为了使其能够编译).我只有一个错误:

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:错误LNK2019: 未解析的外部符号"public: 虚拟__thiscall Game ::〜Game(void) (?? 1Game @@ UAE @ XZ)在 函数"public:虚拟__thiscall TestGame :: ~~ TestGame(void) (?? 1TestGame @@ UAE @ XZ)

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)

这是我的班级定义:

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.

推荐答案

您实际上需要为基类定义析构函数,即使它是纯虚拟的,因为它会在派生类被销毁时被调用.

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.

virtual ~Game() { /* Empty implementation */ }

纯虚拟的= 0是不必要的,因为您还有其他纯虚拟函数可以使您的类抽象化.

The = 0 for pure virtual is not necessary, since you have other pure virtual functions to make your class abstract.

这篇关于错误LNK2019-抽象类中的虚拟析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆