In my class I defined an enum like this:
class myClass { public: enum access { forL, forM, forA }; typedef access AccessType; AccessType aType; };
Later in defined an object like this:
myClass ob; ob->aType = 0;
However I get this error:
error: invalid conversion from 'int' to 'myClass::AccessType {aka myClass::access}' [-fpermissive]
Don't enum fields map to integers?
No, they are stored as integers but they are distinct types (e.g. you can even overload based on the enum type). You must convert explicitly:
myClass ob; ob->aType = (myClass::AccessType)0;
or ever better write the corresponding named value of the enum:
myClass ob; ob->aType = myClass::forL;
Or perhaps if you want to use the enum just as a set of integer constants, change the type of the field:
class myClass { public: enum { forL, forM, forA }; int aType; // just stores numbers };
Conversion from enum to int is implicit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With