My enum is not a class or namespace(我的枚举不是类或命名空间)
问题描述
我有名为 MyCode.h 和 MyCode.cpp 的文件
Hi I have files called MyCode.h and MyCode.cpp
在 MyCode.h 中我已经声明
In MyCode.h I have declared
enum MyEnum {Something = 0, SomethingElse = 1};
class MyClass {
MyEnum enumInstance;
void Foo();
};
然后在 MyCode.cpp 中:
Then in MyCode.cpp:
#include "MyCode.h"
void MyClass::Foo() {
enumInstance = MyEnum::SomethingElse;
}
但是当使用 g++ 编译时,我收到错误MyEnum"不是类或命名空间...
but when compiling with g++ I get the error 'MyEnum' is not a class or namespace...
(在 MS VS2010 中工作正常,但在 linux g++ 中无效)
(works fine in MS VS2010 but not linux g++)
有什么想法吗?谢谢托马斯
Any ideas? Thanks Thomas
推荐答案
语法 MyEnum::SomethingElse
是 Microsoft 的扩展.它恰好是我喜欢的一种,但它不是标准 C++.enum
值被添加到周围的命名空间:
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;
}
这篇关于我的枚举不是类或命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我的枚举不是类或命名空间


基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01