Hi I have files called MyCode.h and MyCode.cpp
In MyCode.h I have declared
enum MyEnum {Something = 0, SomethingElse = 1}; class MyClass { MyEnum enumInstance; void Foo(); };
Then in MyCode.cpp:
#include "MyCode.h" void MyClass::Foo() { enumInstance = MyEnum::SomethingElse; }
but when compiling with g++ I get the error 'MyEnum' is not a class or namespace...
(works fine in MS VS2010 but not linux g++)
Any ideas? Thanks Thomas
An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).
Description. The java.lang.Enum.name() method returns the name of this enum constant, exactly as declared in its enum declaration.
Your code (e.g. your enum) SHOULD be placed in the . h file if you need to expose it to the code you're including the . h file. However if the enum is only specific to the code in your header's .
Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.
The syntax MyEnum::SomethingElse
is a Microsoft extension. It happens to be one I like, but it's not Standard C++. enum
values are added to the surrounding namespace:
// header enum MyEnum {Something = 0, SomethingElse = 1}; class MyClass { MyEnum enumInstance; void Foo(); } // implementation #include "MyClass.h" void Foo() { enumInstance = SomethingElse; }
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