Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My enum is not a class or namespace

Tags:

c++

enums

c++11

g++

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

like image 787
friartuck Avatar asked Mar 04 '11 01:03

friartuck


People also ask

Is enum class or data type?

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

How do I find the class name of an enum?

Description. The java.lang.Enum.name() method returns the name of this enum constant, exactly as declared in its enum declaration.

Do enums go in header files?

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 .

Can a class contain enum?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.


1 Answers

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;  } 
like image 82
Max Lybbert Avatar answered Oct 02 '22 20:10

Max Lybbert