您好,我有名为 MyCode.h 和 MyCode.cpp 的文件
在 MyCode.h 我已经声明
enum MyEnum {Something = 0, SomethingElse = 1};
class MyClass {
MyEnum enumInstance;
void Foo();
};
然后在 MyCode.cpp 中:
#include "MyCode.h"
void MyClass::Foo() {
enumInstance = MyEnum::SomethingElse;
}
但是当使用 g++ 编译时,我得到错误 'MyEnum' is not a class or namespace...
(在 MS VS2010 中运行良好,但在 linux g++ 中运行良好)
有什么想法吗? 谢谢 托马斯
最佳答案
语法 MyEnum::SomethingElse
是 Microsoft 扩展。它恰好是我喜欢的一种,但它不是标准 C++。 enum
值被添加到周围的命名空间:
// header
enum MyEnum {Something = 0, SomethingElse = 1};
class MyClass {
MyEnum enumInstance;
void Foo();
}
// implementation
#include "MyClass.h"
void Foo() {
enumInstance = SomethingElse;
}
关于c++ - 我的枚举不是类或命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5188554/