数组枚举和输入
Arrays enums and input
我有一个枚举的数据类型。我想从 main 内部设置/分配其值。我的代码看起来像
class pet
{
public:
enum species { bird,cat,dog,rat,horse };
enum demands { food,water,companionship };
demands demand;
species petSpecies;
float waterLevel = 100;
float foodLevel = 100;
float Happiness = 100;
//Species are not initialized in the class and have to be initialized separately in main
string name;
//And a bunch of functions here and there
};
在我的主函数中,我使用矢量来创建一组宠物,每个宠物都有自己的名称和物种。事实证明,为宠物设定一个物种是很困难的。
我该怎么做?
vector_pets[i].petSpecies = pet::horse;
应该做你想做的事。
如果你在类中声明你的枚举,你必须使用类的名称+作用域
这里以为例
pet::horse
您还可以在类外部声明枚举
相关文章:
- 不带大括号的枚举形式
- 枚举环境变量的惯用C++14/C++17方法
- 类似枚举的计算常量
- 如何正确实现和访问运算符的各种自定义枚举器
- 错误:从"int"到枚举c++的转换无效
- C++中构造函数中的枚举
- 带有 c++ 的枚举(输入检查)
- 用于获取枚举值的用户输入
- 从 cin 获取 c++ 中结构中多个枚举的输入
- 重载输入 (>>) 运算符以获取枚举字符串
- 数组枚举和输入
- 从用户输入中提取整数的枚举开关语句
- 如何在 c++ 中将枚举与用户输入一起使用
- 在类中使用枚举为用户输入指定值选项
- C ++枚举并使用它们从用户输入
- C++ 简单的输入/输出程序 + 枚举器
- 将键盘输入与枚举类型数据[C++]匹配
- 枚举作为构造函数c++eclipse的输入
- 如何确保用户输入允许的枚举
- 将 c++ 字符串映射到枚举以便获取用户输入