Jump to content
  • Advertisement
Sign in to follow this  
steeg

Abstract class instantiation problem

This topic is 4542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!