将值设置为同一名称空间但不同类中的枚举? C++
编辑 感谢问题下的评论,我意识到您还必须在头文件中声明一个枚举。 >。<为什么互联网上没有任何关于枚举的内容提到这一点? 现在编译器正在识别地质学家。
我的枚举位于名为 GameModeState
的类中的 namespace Star
中,但我需要检查名为 ZoneMovementState
的类中的当前枚举值,该类也是使用命名空间Star
。我将 GameModeState 包含在 ZoneMovementState 的顶部。 GameModeState 中的枚举声明是这样的:
enum Job {Landman = 0, Geologist = 1};
我正在尝试在 ZoneMovementState 中使用此代码:
int placeholderJob = Star::GameModeState::Geologist;
//or I've tried this
int placeholderJob = GameModeState::Geologist;
出于某种原因,我的编译器在任何一次尝试中都无法识别地质学家;如何将 placeholderJob
设置为地质学家?
EDIT
Thanks to comments under the question, I realized that you have to declare an enum in the header file also. >.< Why does nothing on the internet about enums mention this?
Now the compiler is recognizing Geologist.
My enum is within namespace Star
in a class called GameModeState
but I need to check the current enum value within a class called ZoneMovementState
, which is also using namespace Star
. I have GameModeState included at the top of ZoneMovementState.
The enum declaration in GameModeState is this:
enum Job {Landman = 0, Geologist = 1};
I'm trying to use this code in ZoneMovementState:
int placeholderJob = Star::GameModeState::Geologist;
//or I've tried this
int placeholderJob = GameModeState::Geologist;
For some reason my compiler is not recognizing Geologist in either attempt; how do I set placeholderJob
to Geologist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不承认您的计划范围内的地质学家吗? (当你将鼠标悬停在上面时,智能感知会弹出并显示 Geographer 是一个等于 1 的枚举类型)还是它下面有一个波浪线(表明它无法识别该类型?)
这可能是一个范围问题(尽管基于您的信息听起来不像),或者您使用的编译器可能不允许在没有显式强制转换的情况下将枚举的值设置为整数。
Does it not recognize Geologist in the scope of your program? (When you mouse over does intellisense pop up and show you that Geologist is an enum type equal to 1) or does it have a squigly underneath it (indicating it does not recognize the type?)
This could be a scoping issue (although based on your information it doesn't sound like it), or perhaps the compiler you are using does not allow setting the value of an enumeration to an integer without an explicit cast.
互联网上不需要提及这一点,因为它对编译单元很熟悉。
头文件(基本上)告诉编译器存在哪些名称(标识符)以及它们代表什么。这就是为什么编译器在不知道地质学家代表什么时告诉您的原因。
函数、字段、类、结构、typedef、命名空间也是如此,所以问题实际上是
The internet doesn't need to mention this, because it groks compilation units.
Header files are there to tell a compiler (basically) what names (identifiers) exist and what they represent. This is why the compiler tells you when he doesn't know what a Geologist represents.
The same goes for functions, fields, classes, structs, typedefs, namespaces, so really the question would be