Abstract class instantiation problem

Recommended Posts

steeg    150
I have an abstract GameState class and several concrete states classes which are implemented as singletons, here is the code:
CGameState (GameState.h)
#include "GameEngine.h"

class CGameState {
public:
virtual void handleEvents(CGameEngine* game) = 0;
virtual void update(CGameEngine* game) = 0;
virtual void draw(CGameEngine* game) = 0;
~CGameState(void);
protected:
CGameState() { }
};


CIntroState (CIntroState.h)
#include "GameState.h"

class CIntroState : public CGameState {
public:
void HandleEvents(CGameEngine* game);
void Update(CGameEngine* game);
void Draw(CGameEngine* game);
static CIntroState* Instance() {
return &m_IntroState;
}

protected:
CIntroState() { }
private:
static CIntroState m_IntroState;
};


CIntroState (CIntroState.cpp)

#include "GameEngine.h"
#include "GameState.h"
#include "IntroState.h"

CIntroState CIntroState::m_IntroState;   // <-- COMPILER ERROR HERE

void CIntroState::HandleEvents(CGameEngine* game){
}

void CIntroState::Update(CGameEngine* game) {
}

void CIntroState::Draw(CGameEngine* game) {
}


Where marked in the code, I am getting the following problem 'CIntroState' : cannot instantiate abstract class. I must be doing something wrong when trying to initialize the concrete IntroState class, I don't understand why it says it cannot instantiate an abstract class since CIntroState itself is not abstract. Any help is appreciated. Thanks.

Share on other sites
Capitalization. You have misspelled the derived classes' methods.

Share on other sites
jonahrowley    300
Method names are case-sensitive. Fix that, and it'll work. Also, your overrided member functions should also be declared virtual.

Share on other sites
steeg    150
ohh..thats embarrassing lol...thanks

Share on other sites
rip-off    10976
It is considered a good idea to declare the destructor of any class with one or more virtual methods to be virtual aswell. This will not affect you at the moment, but if you ever decide to change your system ( notably remove the singletons ) then it could be a problem.

Share on other sites
steeg    150
Quote:
 Original post by rip-offIt is considered a good idea to declare the destructor of any class with one or more virtual methods to be virtual aswell. This will not affect you at the moment, but if you ever decide to change your system ( notably remove the singletons ) then it could be a problem.

will take that into account.