Sign in to follow this  
steeg

Abstract class instantiation problem

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by rip-off
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.

will take that into account.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this