我的枚举不是类或命名空间 [英] My enum is not a class or namespace

查看:25
本文介绍了我的枚举不是类或命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有名为 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;
 }

这篇关于我的枚举不是类或命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆