enum in class

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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Room
{
    public:
        Room();
        virtual ~Room();
        float Getm_temp() { return m_temp; }
        void Setm_temp(float val) { m_temp = val; }
        int Getm_wind() { return m_wind; }
        void Setm_wind(int val) { m_wind = val; }
        int Getm_humidity() { return m_humidity; }
        void Setm_humidity(int val) { m_humidity = val; }
        bool Getm_rain() { return m_rain; }
        void Setm_rain(bool val) { m_rain = val; }
        enum ExitDirection_t{North, NorthWest, West, SouthWest, South, SouthEast, East, NorthEast };
    protected:
    private:
        float m_temp;
        int m_wind;
        int m_humidity;
        bool m_rain;
        ExitDirection_t exit;
};

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.

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
#include <iostream>
#include "header_file_32478.h" //Has all include files

using namespace std;

int main()
{
    Room lobby;//Rom's parent is Wold_Coord class
    lobby.ExitDirection_t = "North";//was trying to test calling enum
    return 0;
}

//enum in this class
#ifndef ROOM_H
#define ROOM_H

#include "World_Coord.h"//Room's parent

class Room: public World_Coord
{
    public:
        Room();
        virtual ~Room();
        float Getm_temp() { return m_temp; }
        void Setm_temp(float val) { m_temp = val; }
        enum ExitDirection_t{North, NorthWest, West, SouthWest, South, SouthEast, East, NorthEast };//enum in question
    protected:
    private:
        float m_temp;
        ExitDirection_t exit;//enum in question 


Please help me! :(

addendum:
I changed the room class to this in hopes of making it work. Am I understanding it better, after so much reading my eyes are bleeding?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#ifndef ROOM_H
#define ROOM_H

#include "World_Coord.h"
using namespace std;

class Room: public World_Coord
{
public:
    Room();
    virtual ~Room();
    bool Getm_rain(){return m_rain;}
    void Setm_rain(bool val){m_rain = val;}
    enum ExitDirection{North, NorthWest, West, SouthWest, South, SouthEast, East, NorthEast };
    void SetExit(ExitDirection direction){m_exit = direction;}
    int GetExit(void){return m_exit;}
protected:
private:
    bool m_rain;
    ExitDirection m_exit;
};

#endif // ROOM_H 
Last edited on
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;
Last edited on
I edited the second code listing above. I am trying to set the direction in main() to be called later by whatever means necessary like this
1
2
    Room lobby;
    lobby.SetExit(North);

But the compiler is saying North was not declared in this scope. What am I doing wrong now?
lobby.SetExit( Room::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:
1
2
3
4
if(lobby.GetExit() == 0)
    cout << "North!";
    else
    cout << "Not north!";

I assigned the cardinal directions their values according to degrees:
North = 1, ... West = 270, ... NorthWest = 315 etc...
Last edited on
hello lare26,

lare26 wrote:
I will always have to use an integer to compare it right?
no. you better write if(lobby.GetExit() == Room::North) or even using switch
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.
Last edited on
ok, to brighten it a bit.

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

hope that makes it a bit more clear
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;
else if( dir == APPLE )  // !?!?
    std::cout << "er, exactly what direction is an apple??" << std::endl;


Enums solve the above problem, nothing more.
Last edited on
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.
Topic archived. No new replies allowed.