开发者

Setting a value to an enum in same namespace but different class? C++

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};
开发者_如何学JAVA

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?


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜