3

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.

Community
  • 1
  • 1
ptpaterson
  • 9,131
  • 4
  • 26
  • 40
  • Note the `virtual` keywords in the derived class are optionnal regarding to the standard. Maybe are they mnemotechnic, thats OK. – Sandburg Sep 21 '18 at 15:19

1 Answers1

12

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 */ }

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

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • I assume you meant "destructor". – Edward Strange Dec 07 '10 at 18:29
  • 1
    You don't need the =0 if you also provide the empty implementation (though it is not illegal, just slightly confusing to most). – Martin York Dec 07 '10 at 18:32
  • @Martin York: you need the =0 if it's the only pure virtual method, to ensure the class is abstract, to prevent instantiation. – Yttrill Dec 07 '10 at 19:10
  • 1
    First, drop that "void", C++ doesn't need it. Second, you can't have pure virtual declaration (=0) and definition at the same place. You must provide definition outside of class (inline or not - your choice): Game::~Game() {} – Gene Bushuyev Dec 07 '10 at 19:23
  • Thanks guys. I ~Game(){} in the implementation and it works fine. – ptpaterson Dec 07 '10 at 19:34
  • @ptpaterson: Do keep it `virtual`, though! It just doesn't need the `=0`. – Fred Larson Dec 07 '10 at 21:09
  • @Gene: You're right. I thought `= 0` worked there but it doesn't. I think `inline` needs to be on the definition if it's in a header file to avoid problems with the ODR. – Fred Larson Dec 07 '10 at 21:25