Class does not expose public enum...?

Nov 23, 2008 at 12:32pm
Hi,

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


and Game.cpp which looks like this:

1
2
3
4
5
6
7
8

#include "Game.h"

Game::enumMainState Game::getMainState()
{

}



However, when I compile I get "error: ‘enumMainState’ does not name a type". Can anyone tell me where I'm going wrong?

Thanks in advance.
Nov 23, 2008 at 4:05pm
your Game.cpp declaration is slightly off, the return type doesn't match the one in your header file is what its trying to say:

enumMainState Game::getMainState() {}
Last edited on Nov 23, 2008 at 4:05pm
Nov 23, 2008 at 4:16pm
Excellent, cheers for that! For anyone else wondering, I changed the header file definition to:

 
Game::enumMainState getMainState();


And it works fine.
Nov 23, 2008 at 4:39pm
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.

Nov 23, 2008 at 4:51pm
I omitted a lot of the irrelevant code, so if you are interested here are the complete files:

Game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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_*/


Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "Game.h"


Game::enumMainState Game::getMainState()
{
	return mainState;
}

void Game::setMainState(enumMainState newState)
{
	mainState = newState;
}

PlayerState* Game::getPlayerState()
{
	return player;
}

NPCStateCollection* Game::getNPCStateCollection()
{
	return npcStateCollection;
}

void Game::start()
{
	//Initialise
	
	while (mainState != stopped)
	{
		//Capture input
		//Move gamestate on one frame
		//Render video
		//Render sound
	}
	
	//Cleanup
}


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.
Last edited on Nov 23, 2008 at 4:52pm
Nov 23, 2008 at 6:15pm
That is bizarre, because line 30 in the header file is not legal syntax AFAIK. (I know older versions of GCC allow it though).
Nov 23, 2008 at 6:28pm
I would just like to point out that the original code compiled with no errors for me in GCC 3.4.5.
Nov 23, 2008 at 10:02pm
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.
Topic archived. No new replies allowed.