C++ 头文件中的枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9220678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:30:22  来源:igfitidea点击:

Enum in header files

c++enumsheaderscope

提问by drCoding

I have enum declared in a header file called "sm.h"

我在名为“sm.h”的头文件中声明了枚举

enum GameStates
{
  STATE_NULL = 0,
  STATE_INTRO,
  STATE_TITLE,
  STATE_MAIN,
  STATE_EXIT
};

All it does is list the possible game states

它所做的只是列出可能的游戏状态

However in the following line in "base.cpp":

但是在“base.cpp”中的以下行中:

stateID = STATE_INTRO;

The compiler says "STATE_INTRO was not declared in this scope". I have no idea what I am doing wrong. I know that I have included the header file right and I can go to its deceleration from the .cpp file. So why am I getting this error.

编译器说“STATE_INTRO 未在此范围内声明”。我不知道我做错了什么。我知道我已经正确地包含了头文件,我可以从 .cpp 文件中减速。那么为什么我会收到此错误。

stateID = STATE_INTRO;

Is used in:

用于:

bool baseFunctions::load_rc()
{
 stateID = STATE_INTRO;

 currentState = new Intro();

 return true;
}

which defines a class function in a header file.

它在头文件中定义了一个类函数。

There are no global conflicts because it is the only enum in the whole program

没有全局冲突,因为它是整个程序中唯一的枚举

回答by Joel Rondeau

From your link to your files, you have the following in both sm.hand base.h

从您的文件链接,您在两个sm.hbase.h

#ifndef BASE_H_INCLUDED
#define BASE_H_INCLUDED

Change the one in sm.hto something like

将其中一个更改为sm.h类似

#ifndef SM_H_INCLUDED
#define SM_H_INCLUDED

and I expect you'll be fine.

我希望你会没事的。

As it is, base.cpploads base.h, then when it gets to sm.hthe #ifndef is false, so it excludes everything in sm.h.

实际上,base.cpploads base.h,然后当它到达sm.h#ifndef 时为false,因此它排除了.js 中的所有内容sm.h

回答by Martin Beckett

Most likely is that you aren't including "sm.h" in base.cpp

很可能是您没有在 base.cpp 中包含“sm.h”