gpt4 book ai didi

c++ - 作用域枚举

转载 作者:行者123 更新时间:2023-11-30 01:52:30 25 4
gpt4 key购买 nike

我正在开发一个简单的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com