My enum is not a class or namespace

http://stackoverflow.com/questions/5188554/my-enum-is-not-a-class-or-namespace


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?ThanksThomas

+++++++++++++++++++++++++++++


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;
 }


posted @ 2016-04-06 14:05  张同光  阅读(303)  评论(0编辑  收藏  举报