Help With Enums

I just can't seem to get enums in other classes located in other files. I do not get why this will not work.
Main File
1
2
3
4
5
#include "OtherFile.hpp"
void Function(int Type){
		Class::Enum ThisEnum;
		ThisEnum = Type; //Assuming input is valid
}


OtherFile
1
2
3
4
class Class{
public:
enum Enum {A = 1, B = 2, C = 3};
};


Now the error here is "A value of 'int' can
not be assigned to the entity type 'Class::Enum'

But, enums are able to take integer input right? Do I need to do some sort of
Enum cast to the integer?

Thanks in advance!
Last edited on
But, enums are able to take integer input right? Do I need to do some sort of Enum cast to the integer?


No. Enums are not integers, but they can be converted to them.

The reason why you can't do this is because assigning say, 5 to your enum in this case would give you strange results as 5 is not defined.

IIRC you can usually cast the int directly to the enum type if you need this.
Besides checking for invalid numbers all I would do is just:
 
ThisEnum = enum(Type);

Which gives me an 'Invalid type'

Ty for the Speedy Response
Last edited on
Is enum an advanced definition of #define?
http://en.wikipedia.org/wiki/C%2B%2B11#Strongly_typed_enumerations


I was just reading about the new stuff in C++11.

@JM are you going to have facility to deal with enums (amongst all the other things) in your scripting / interpreter / child language or whatever it is you are trying to do? That is, once you have learnt what they are, and why you don't use #define.
Last edited on
How would I cast an integer to a enum in c++03 though?
ThisEnum = Class::Enum(Type);

Just use the correct type.
Thanks TheIdeasMan...
Last edited on
Ok, thanks. That makes sense.
As firedraco has already said, it is not a good idea to cast an int to an enum as you can end up with a meaningless value.

It would better to use a helper function to do the conversion, so that meaningless values can be ignored (you return a default value) or you report an error.

e.g. where the method is declared in Class as:

static Enum IntToEmum(int Type);

it could be

1
2
3
4
5
6
Class::Enum Class::IntToEmum(int Type){
    // if enum is contiguous
    if((1 <= Type) && (Type <= 3))
        return static_cast<Enum>(Type); // new-style C++ casts are better
    return A; // return default value if int value is invalid
}


or

1
2
3
4
5
6
7
8
9
10
Class::Enum Class::IntToEmum(int Type){
    // handles non-contigous case, e.g. Enum {A = 1, B = 3, C = 5};
    switch(Type) {
        case A: return A;
        case B: return B;
        case C: return C;
        default {/* do nothing */}
    }
    throw invalid_enum(); // throw an exception
}


and you use it like

1
2
3
4
5
#include "OtherFile.hpp"
void Function(int Type){
    Class::Enum ThisEnum;
    ThisEnum = Class::InToEnum(Type);
}


I assume that Function is being used in another .cpp file which doesn't include OtherFile.hpp.

Andy


Last edited on
Topic archived. No new replies allowed.