| Pickle Gunner (63) | |||||
|
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
OtherFile
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
|
|||||
| firedraco (5413) | ||
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. | ||
|
|
||
| Pickle Gunner (63) | |||
Besides checking for invalid numbers all I would do is just:
Which gives me an 'Invalid type' Ty for the Speedy Response | |||
|
Last edited on
|
|||
| Jackson Marie (462) | |
| Is enum an advanced definition of #define? | |
|
|
|
| TheIdeasMan (1564) | ||
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
|
||
| Pickle Gunner (63) | |
| How would I cast an integer to a enum in c++03 though? | |
|
|
|
| cire (1850) | |
ThisEnum = Class::Enum(Type); Just use the correct type. | |
|
|
|
| Jackson Marie (462) | |
| Thanks TheIdeasMan... | |
|
Last edited on
|
|
| Pickle Gunner (63) | |
| Ok, thanks. That makes sense. | |
|
|
|
| andywestken (1950) | |||||||
|
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
or
and you use it like
I assume that Function is being used in another .cpp file which doesn't include OtherFile.hpp. Andy | |||||||
|
Last edited on
|
|||||||