I have a class definition in a header file Game.h which looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Game {
public:
/// Enumeration of the possible overall states of the game.
enum enumMainState {
started,
playing,
paused,
cutscene,
stopped
};
enumMainState getMainState();
private:
enumMainState mainState;
};
I'm confused as to how the solution solves a problem I didn't think existed in the first place? The original code looked right to me. enumMainState is scoped within Game, so Game.cpp has to be written like the poster original wrote.
But changing the declaration in the header file to
Game::enumMainState getMainState();
is not standards compliant and should (at least on newer compilers) give a compile error.
#ifndef GAME_H_
#define GAME_H_
#import "PlayerState.h"
#import "NPCStateCollection.h"
/** Represents the overall game state. This is the central class of the game,
as it will initialise all subsystems and begin the game loop.
*/
class Game {
public:
/// Enumeration of the possible overall states of the game.
enum enumMainState {
///The initial state. Indicates that state is currently undefined and that all resources must be initialised.
started,
///'Normal' mode, indicating that gameplay is underway.
playing,
///Gameplay is frozen until state is changed, no other input is accepted other than state changes.
paused,
///GUI is displayed on screen, all input should be directed here.
gui,
///A cutscene is displayed on screen, all input should be directed here.
cutscene,
///The game will terminate.
stopped
};
/// Returns the overall state of the game. See enumMainState for details of the meanings of different states.
Game::enumMainState getMainState();
/// Sets the overall state of the game. See enumMainState for details of the meanings of different states.
void setMainState(enumMainState mainState);
/// Returns the PlayerState object which represents the player.
PlayerState* getPlayerState();
/// Returns the NPCStateCollection object which stores all Non-Playable Characters.
NPCStateCollection* getNPCStateCollection();
/// Starts the game loop and begins game play.
void start();
private:
enumMainState mainState;
PlayerState* player;
NPCStateCollection* npcStateCollection;
};
#endif /*GAME_H_*/
If I remove the Game:: in front of the method definition in Game.cpp, I get the aforementioned error. Same thing happens if I remove the Game:: in Game.h. I am using GCC 4.2.4 from inside Eclipse 3.3.2.
Thanks for replying everyone. As I want to avoid writing non-standard code I messed around a bit more, and it seems to be Eclipse getting confused. I tried editing the files in Vim and ran make from the command line and it worked fine, and now Eclipse doesn't have a problem. However I know for a fact that the only way it worked in my last session was as described above because I was spending so long fiddling with it! Its good to know which method is the 'proper' way so cheers for that.