c++ - 我的枚举不是类或命名空间

标签 c++ enums g++ c++11

您好,我有名为 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/

相关文章:

c++ - 为什么 C++ 使用 throw() 关键字来指示函数不会抛出任何异常

c++ - 读取文件时找到文件末尾

c++ - 为什么我更喜欢 "explicitly typed initializer"成语而不是明确给出类型

c++ - 为C++中的所有枚举类添加方法专门化

c++ - 关于如何在基于动态数组的结构中存储键值对的想法

linux - 在 code::blocks linux 编译器中,如何添加编译标志,如 '-lrt' 或 '-lboost_thread'?

C++编译时错误: expected identifier before numeric constant

c++ - [Qt][Linux] 列出驱动器或分区

c++ - 将枚举字符串(不是值)传递给宏

c++函数返回一个枚举?