Strange behaviour of pointers to derived class

Started by
5 comments, last by wqking 11 years, 9 months ago
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]


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)
Advertisement
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
game->ChangeState(game->play_state);
you can have something like
game->ChangeState(game_state_vector[PLAY_STATE]);
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?

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.
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.

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
game->ChangeState(game->play_state);
you can have something like
game->ChangeState(game_state_vector[PLAY_STATE]);
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?

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


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.

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.
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.
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.

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;
}

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Are your forward declaring PlayState?
If so, the compile won't know it's derived from GameState.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

This topic is closed to new replies.

Advertisement