作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个简单的 Breakout 克隆来练习我初出茅庐的 C++ 技能。游戏的资源是“资源”类的子类,该类包含用于识别资源类型的 ID 和一些虚拟函数。到目前为止,类看起来像这样:
class Resource {
private:
int _id;
protected:
enum ResourceType {
TEXT
, PADDLE
, BALL
, BRICK
};
public:
Resource(int resourceID) : _id{resourceID} {}
};
但是,为了熟悉 C++11,我想切换到作用域枚举。我确定我在这里遗漏了一些东西(毕竟代码不会编译!)。错误修改代码如下:
class Resource {
private:
ResourceType _id;
protected:
enum class ResourceType : int {
TEXT = 0
, PADDLE
, BALL
, BRICK
};
public:
Resource(ResourceType resourceID) : _id{resourceID} {}
};
考虑到我收到的编译器错误(“错误:'ResourceType' 没有命名类型”在 ResourceType _id;
行),看来我可能需要制作我的枚举类的原型(prototype)其他地方,但我的尝试是徒劳的。请指教!
最佳答案
您只需在使用前声明类型,如下所示:
class Resource {
protected:
enum class ResourceType : int {
TEXT = 0
, PADDLE
, BALL
, BRICK
};
private:
ResourceType _id;
public:
Resource(ResourceType resourceID) : _id{ resourceID } {}
};
关于c++ - 作用域枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24477866/
我是一名优秀的程序员,十分优秀!