Jump to content

  • Log In with Google      Sign In   
  • Create Account


Strange behaviour of pointers to derived class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Maeriden   Members   -  Reputation: 140

Like
0Likes
Like

Posted 28 June 2012 - 07:20 PM

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*)


'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, 28 June 2012 - 07:29 PM.


Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 11877

Like
1Likes
Like

Posted 28 June 2012 - 08:02 PM

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?

#3 King Mir   Members   -  Reputation: 1910

Like
0Likes
Like

Posted 28 June 2012 - 08:48 PM

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.

#4 Maeriden   Members   -  Reputation: 140

Like
0Likes
Like

Posted 28 June 2012 - 08:59 PM

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.

Edited by Maeriden, 28 June 2012 - 09:32 PM.


#5 ankhd   Members   -  Reputation: 1100

Like
0Likes
Like

Posted 02 July 2012 - 02:04 AM

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.

#6 NightCreature83   Crossbones+   -  Reputation: 2671

Like
0Likes
Like

Posted 02 July 2012 - 03:12 AM

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

Edited by NightCreature83, 02 July 2012 - 03:13 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#7 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 02 July 2012 - 04:15 AM

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

Edited by wqking, 02 July 2012 - 04:15 AM.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS