Sign in to follow this  

Strange behaviour of pointers to derived class

This topic is 1989 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 am trying to make a game states system.
I made a virtual base class 'GameState' and derive the various states from it. The states are pushed in a std::vector<GameState*>

At first I declared the class holding the vector, the GameState base class, and derived states classes all in the same header and implemented them in the same source file, and everything was ok. There is a function called by the states that changes the current state

[source lang="cpp"]void ChangeState(GameState* newstate)[/source]

Then I decided to put each class in a separate header-source and the problem appeared.
If i try to pass to it, say, a pointer to an instance of class PlayState (derived from GameState) the compiler gives the following error

[source lang="cpp"]game->ChangeState(game->play_state)[/source]

[quote]
error: no matching function for call to 'GameManager::ChangeState(PlayState*&)'
note: candidates are: void GameManager::ChangeState(GameState*)
[/quote]

'game' is a pointer to GameManager that every state has
GameManager holds the vector and pointers to states' instances (like play_state).

Trying random stuff, I noticed that if I include the headers of each derived state in GameManager.h (instead of GameManager.cpp) the problem gets fixed (at first I just forward declared the classes since GameManager holds only pointers).

I'd like help in understanding what's going on. Sorry if the explanation isn't very clear but I have trouble identifying the issue myself and I didn't want fill the post with all the headers (I'll do it if it's necessary though) Edited by Maeriden

Share this post


Link to post
Share on other sites
My guess is that when you are not including all the headers, you are using forward class declarations, and at the point of the error the compiler doesn't know that PlayState derives from GameState.

Ideally the vast majority of your code shouldn't know anything at all about the derived classes, so instead of
[code]game->ChangeState(game->play_state);[/code]
you can have something like
[code]game->ChangeState(game_state_vector[PLAY_STATE]);[/code]
where `PLAY_STATE' is a value in an enum and `game_state_vector' is the vector of pointers to GameState that you described.

Would something like that work?

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1340935356' post='4953809']
My guess is that when you are not including all the headers, you are using forward class declarations, and at the point of the error the compiler doesn't know that PlayState derives from GameState.
[/quote]Sounds right. Except where not possible, or as a way of API protection, you want to include the class headers that you have pointers to. In this case you need to include PlayState.h, which should define PlayState, when you're using a PlayState pointer.

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1340935356' post='4953809']
My guess is that when you are not including all the headers, you are using forward class declarations, and at the point of the error the compiler doesn't know that PlayState derives from GameState.

Ideally the vast majority of your code shouldn't know anything at all about the derived classes, so instead of
[code]game->ChangeState(game->play_state);[/code]
you can have something like
[code]game->ChangeState(game_state_vector[PLAY_STATE]);[/code]
where `PLAY_STATE' is a value in an enum and `game_state_vector' is the vector of pointers to GameState that you described.

Would something like that work?
[/quote]
That doesn't really work because the states are not already in the vector, ChangeState() pushes them into it (honestly I would have used a different system which doesn't involve a vector at all, but the tutorials on internet say to do it like this, and supposedly they know better than me, so...)

But I think you nailed the problem! In the implementation file for each state I didn't include the header of the other state that was being passed to ChangeState(), so I guess it couldn't see that it was derived from GameState, it would see it as a, say, PlayState pointer because it is declared like that in GameManager.h.
Including the states headers in GameManager.h made it work because then each state header, by including GameManager.h, would have included the other states headers as well

Thank you very much

[quote name='King Mir' timestamp='1340938082' post='4953815']
Sounds right. Except where not possible, or as a way of API protection, you want to include the class headers that you have pointers to. In this case you need to include PlayState.h, which should define PlayState, when you're using a PlayState pointer.
[/quote]
I'll keep that in mind.
Since in the source file where ChangeState() is defined the states headers are included, and it is the same file where the states are instantiated, ChangeState() knows that they are derived and I thought it was enough to make it work. Edited by Maeriden

Share this post


Link to post
Share on other sites
Hello.
I just think you need to pass a pointer to the base class to the function GameManager::ChangeState(baseclass*&)
and then have the virtual ChangeState(foo *) in the base class.

Share this post


Link to post
Share on other sites
Wouldn't it be better to pass an enum as suggested or even a string which is the states id and then have the GameStateManager retrieve the correct state and change to it. I prefer the string method over the enum version because it allows you to have multiple instances of for example a PlayState in the system where the enum only allows one.

GameStateManager code would contian a container like this and some code like this.
[code]
typedef std::map<std::string, GameState*> GameStateMap;
GameStateMap m_gameStates;

bool changeState(const std::string& stateNameToChangeTo)
{
GameStateMap::iterator gameStateToFind = m_gameStates.find(stateNameToChangeTo);
if (gameStateToFind != m_gameStates.end())
{
m_currentState = gameStateToFind->second;
return true;
}
return false;
}
[/code] Edited by NightCreature83

Share this post


Link to post
Share on other sites
Are your forward declaring PlayState?
If so, the compile won't know it's derived from GameState. Edited by wqking

Share this post


Link to post
Share on other sites

This topic is 1989 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.

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