将值设置为同一名称空间但不同类中的枚举? C++

发布于 2024-11-01 11:21:21 字数 664 浏览 1 评论 0原文

编辑 感谢问题下的评论,我意识到您还必须在头文件中声明一个枚举。 >。<为什么互联网上没有任何关于枚举的内容提到这一点? 现在编译器正在识别地质学家。

我的枚举位于名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱倦旧时光 2024-11-08 11:21:21

它不承认您的计划范围内的地质学家吗? (当你将鼠标悬停在上面时,智能感知会弹出并显示 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.

吾性傲以野 2024-11-08 11:21:21

为什么互联网上没有任何关于枚举的内容提到这一点?

互联网上不需要提及这一点,因为它对编译单元很熟悉。

头文件(基本上)告诉编译器存在哪些名称(标识符)以及它们代表什么。这就是为什么编译器在不知道地质学家代表什么时告诉您的原因。

函数、字段、类、结构、typedef、命名空间也是如此,所以问题实际上是

为什么编译器会神奇地知道另一个编译单元中的枚举,而其他所有内容都必须为他拼写出来?

Why does nothing on the internet about enums mention this?

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

Why would a compiler magically know about enums in another compilation unit, when everything else has to be spelled out for him?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文