2017-04-14 93 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」,現在用戶必須輸入解決該 –