2017-04-14 90 views
0

我有一个枚举数据类型为/在一个类中。我想从主内部设置/赋值。我的代码看起来像数组枚举和输入

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 

}; 

里面我主要的功能,我使用向量来创建一个集中的每个宠物都有自己的名称和种类。证明困难的是给宠物设置一个物种。
我该怎么做?

回答

1

vector_pets[i].petSpecies = pet::horse;应该做你想做的。

1

如果在类中声明你的枚举,你必须使用class +范围

这里例如

pet::horse 

的名字,你也可以声明类地区以外的枚举

+0

如何做到“cin” –

+0

你是什么意思? @SuhridMulay – RomMer

+0

我宣布了一个变量“pet :: species spe”,现在用户必须输入解决该 –