I am creating a program that will test my current "skills" with c++. Here is a short description:
A "city map" with people walking randomly through it. The people will have a few variables which will represent select personality traits. When they meet at the same coords, they interact for a limited number of turns. They then move on, slightly changed by the experience.
This is very loose, of course. I will be adding more detail as I work on it and learn more c++ skills. I want to use enums for room exit directions.(at this time there will be no actual rooms or doors) And, finally, my question is this...Am I using the enum correctly?
And if anyone would like to help tutor me and/or give ideas and some example code to help me figure things out occasionally, I would much appreciate it. If you need to see more of my code, please feel free to ask. I've even started an outline on an online site.
Thanks for taking the time to at least read this. -Lost in Coing Heaven/H-E-double hockey stick.
Grrrr...! I am having a lot of trouble with enums in classes. What am I doing wrong. I just can't wrap my head around this. Eventually choosing the exit will make a person object move to a different coordinate.
lobby.ExitDirection_t = "North";
_ The attribute is exit (ExitDirection_t is its type)
_ Is private, you can't access it outside your class.
_ Is not a string. It can be implicit converted from integers, or It can accept the values North, NorthWest, West, SouthWest, South, SouthEast, East, NorthEast. exit = North;
Woo Hoo!!! Thank you very very much. Phew! This one thing took me all day to figure out. I figured it had to do with that lobby.SetExit(North); One last question about enums in classes: I will always have to use an integer to compare it right?
example:
Thanks coder777 and everyone else too. if(lobby.GetExit() == Room::North) This all has been gold. Maybe now I can understand enum a little better when I read about it again. I could never understand the core of how enum works.
So does anyone want in on the project? If not, do you know where I can go to get some others to help with this and my education. I'm home-schooling myself.
The elements of enum (like North) are labels for numbers. so you can write if(lobby.GetExit() == 0). It's totally allowed to treat them as numbers like int x = Room::North;.
But you can't write ExitDirection_t x = 0; since the compiler considers ExitDirection_t as a special (enum) type. You have to write ExitDirection_t x = (ExitDirection_t) 0; but better not (cast is bad).
you can write enum ExitDirection{North = 1, NorthWest = 270, West = 315, SouthWest, South, SouthEast, East, NorthEast }; then SouthWest => 316, South => 317 since it takes the last number and increments it by one (if no assignment the first number is 0) but you can assigen values to all members of that enum
Put another way, enums work essentially the same as #defines, except they are a little more type safe and they
have scope whereas #defines are always file scope.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Directions
#define NORTH 0
#define SOUTH 180
#define EAST 90
#define WEST 270
/* etc */
// Fruit
#define APPLE 1
#define ORANGE 2
#define BANANA 3
int dir = NORTH;
if( dir == NORTH )
std::cout << "north" << std::endl;
elseif( dir == APPLE ) // !?!?
std::cout << "er, exactly what direction is an apple??" << std::endl;
They also avoid the need to put the values enum fruits{APPLE, ORANGE, BANANA, TOMATO};
And you could think of them more like constants that defines (the pre-processor doesn't touch them).
Thanks, everyone. You have been a big help. I would've avoided putting enum in a class for a long time without all your help. Now on to more work on my project 'World Defined'. I'm glad someone turned me onto this site.