我目前正试图将一个大型的“一体化文件”程序分解成单独的模块,以便更容易使用。我的问题是程序的原始(全部在一个文件中)版本使用了枚举声明。我想在拆分成多个类的程序的新版本中继续使用这个声明,但我不确定这样做的最佳方式。
想到的最简单的解决方案是将枚举 block 放在头文件中并尝试将其用作全局变量(我知道全局变量通常不好,这样使用合适吗?)但是,生成的一堆“错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int”。
我意识到这与之前提出的问题类似。但是,我能找到的所有答案都有些过于技术性或不完全是我想要的。
我的 Global.h 文件和发生错误的文件包含在下面。
#ifndef GLOBAL_H_
#define GLOBAL_H_
#include <SDL.h>
#include "LTexture.h"
#include "LButton.h"
enum LButtonSprite
{
BUTTON_SPRITE_MOUSE_OUT = 0,
BUTTON_SPRITE_MOUSE_OVER_MOTION = 1,
BUTTON_SPRITE_MOUSE_DOWN = 2,
BUTTON_SPRITE_TOTAL = 2
};
.
.
#endif GLOBAL_H_
...
#ifndef LBUTTON_H
#define LBUTTON_H
#include <SDL.h>
#include "Global.h"
class LButton
{
public:
//Initializes internal variables
LButton();
//Sets top left position
void setPosition(int x, int y);
//Handles mouse event
void handleEvent(SDL_Event* e);
//Shows button sprite
void render();
LButtonSprite getCurrSprite();//Problem is here
private:
//Button Position
SDL_Point mPosition;
//Button Sprite
LButtonSprite mCurrentSprite;//And here
};
#endif
首先,是什么导致了这个错误?其次,任何人都可以建议一种更好的方法来实现我正在尝试做的事情吗?
我怀疑编译器将 LButtonSprite 混淆为变量。这是基于 VS 提供的颜色编码(返回类型为蓝色,但 LButtonSprite 为浅蓝色)。从 LButton.h(包含在下面)中可以看出,LButtonSprite 应该被读取为返回类型而不是变量。
谢谢!
最佳答案
如果没有 'extern' 关键字,您发布的内容就可以。
为避免命名冲突,您可以将枚举放在命名空间或类中。
此外,如果 LButton 是唯一使用此枚举的类,那么它也可以在 LButton 本身的类定义中定义。
关于c++ - 重构枚举 block C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20794970/