数组枚举和输入

Arrays enums and input

本文关键字:输入 枚举 数组      更新时间:2023-10-16

我有一个枚举的数据类型。我想从 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

您还可以在类外部声明枚举