Enum Confusion

Hey,

I am having difficulties with using a enum to store the available commands for a Textadventure that I am creating for fun.
I have looked at the two following webpages to help me figure out how to properly use enums to do this task:
http://www.cplusplus.com/doc/tutorial/other_data_types/
http://www.cplusplus.com/doc/tutorial/control/

The following are errors that i don't understand what they are talking about:
Error	6	error C2510: 'directions' : left of '::' must be a class/struct/union	c:\documents and settings\hirokachi\my documents\docs\important junk\textadventure\source\GameField.h	116
Error	4	error C2334: unexpected token(s) preceding '{'; skipping apparent function body	c:\documents and settings\hirokachi\my documents\docs\important junk\textadventure\source\GameField.h	27
Error	5	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\documents and settings\hirokachi\my documents\docs\important junk\textadventure\source\GameField.h	27


The following is the code where the problem lies:
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
//lines 26-27:
class commands {
	enum char { n, s, e, w } command;
};

//lines 58-62:
class GameField {

	std::list<room> listOfRooms;
	std::list<roomConnections> connections;

	// lines 101-121:
	void directionalMove () {
		commands directions;

		//Define the variable storeing player choice
		char move;

		//printRoomDesc (playerPosition.uniqueID);

		//Tell the player they can move now.
		std::cout << "Please enter a direction.\n";

		//get direction from player.
		std::cin >> move;

		//Check if command is valid
		if (directions::n == move) {
			std::cout << "You did try to move North which means that I program correctly. Hurray for me!!!";
		}
		else {
			std::cout << "You didn't try to move north you silly player. Boo, I don't know how to use enums to hold commands.";
		}
	}
};


If anyone is curious I do have this program on Github.com; in fact the following url is the link for said program:
https://github.com/Hirokachi/TextAdventure/tree/RandomMapEnhancement/

if anyone can help or point me in the right direction it would be greatly appreciated.

-Hirokachi

PS: if you need any more information let me know and I will post it here as well. By the way, I am using windows XP sp3 with Microsoft V.S. Express 2010 as the IDE; I am not sure if this is the underling problem.
Last edited on
1
2
3
4
5
class commands 
{
public:
   enum  { n, s, e, w };
};


You can address it as commands::n

It would be better to use descriptive names like NORTH, SOUTH etc.
I used your suggestion on the above code.

In my case, however the following errors are occurring:
Error	1	error C2510: 'directions' : left of '::' must be a class/struct/union	c:\documents and settings\hirokachi\my documents\docs\important junk\textadventure\source\GameField.h	117
Error	2	error C2065: 'north' : undeclared identifier	c:\documents and settings\hirokachi\my documents\docs\important junk\textadventure\source\GameField.h	117


The following is the new code in GameField.h of my project, which has been added for your convenience:
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
//lines 26-29
class commandList {
public:
	enum { north, south, east, west };
};

//in Class GameField and lines 102-126
void directionalMove () {
		commandList directions;

		//Define the variable storeing player choice
		char move;

		//printRoomDesc (playerPosition.uniqueID);

		//Tell the player they can move now.
		std::cout << "Please enter a direction.\n";

		//get direction from player.
		std::cin >> move;

		//Check if command is valid
		if (directions::north == move) {
			std::cout << "You did try to move North which means that I program correctly. Hurray for me!!!";
		}
		else {
			std::cout << "You didn't try to move north you silly player. Boo, I don't know how to use enums to hold commands.";
		}
		//Move player into room from that direction.
		//Print out new Room Description.
		
	}
	
	/*
	* void generateGameMap (totalRooms, randomize):
	*	loads the list of rooms and generates 
	* 	gameMap connections between rooms and randomized
	*	start and end location.
	*/
	void generateGameMap (int totalRooms, bool randomize) {
		
		//Load room description and name/id from file into the listOfRooms.
		
		//for player input:
		//	for the total number of Rooms:
		//		Generate the connections between rooms
		//	Generate start and end positions and set in the room
		//	run test verify if connections create a route to start to end
		// 	store as the current connected gameMap
		//  or load a pregenerated map of said size and store
		
}


Thank you for your suggestions.

-Hirokachi
You need to use commandList::north
Oh so commandList is not technically a class even though it has the class keyword?

I had no Idea! This solution worked!

Thank you so much for your assistance. :D

-Hirokachi
Line 23: It's not clear what value you're expecting the user to enter. north, south, east, west, have the values 0,1,2,3 respectively. That's certainly not clear to the user at line 17 when you ask the user to enter a direction.

Be aware that you can assign character literal values to enums.
1
2
3
4
5
enum { north = 'n', 
             south = 's', 
             east = 'e', 
             west = 'w'
           };

This makes for a more logical input from the user.

so commandList is not technically a class

Yes, commandList is technically a class. It contains one member variable, an anonymous enum.

Not to be confused with C++14 which has strongly typed enums:
 
enum class Color { red, blue };  

http://www.stroustrup.com/C++11FAQ.html#enum
Last edited on
@AbstractionAnon:
Ah thanks for that information and the webpage. That is a very good suggestion.
Topic archived. No new replies allowed.