我正在爲學校做一個小項目,並且需要創建一個具有怪物類型的枚舉,然後是一個函數,它接受一個值並將怪物類型顯示爲一個字符串。這裏是我的代碼位:從枚舉顯示爲字符串的C++名稱
enum MonsterType
{
GHOST,
DRAGON,
GHOUL,
SHRIEKER,
GRIFFIN,
};
string getTypeName()
{
int ID;
cout << "Input Monster ID" << endl;
cin >> ID;
return MonsterType(ID);
}
我得到的錯誤有以下幾種:
no suitable constructor exists to convert from "MonsterType" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>"
和
'return': cannot convert from 'MonsterType' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
我敢肯定有我缺少一個小東西並沒有意識到,如果你能幫助我,我會非常感激。
謝謝
_I'm肯定有一點小小的轉換我不知道_什麼讓你如此肯定,有'MonsterType'和'std :: string'之間的轉換?它應該如何表現? –
不,我的意思是我可以做的轉換。這可能只是我錯過的一件小事,並沒有意識到。 –
[如何將枚舉類型變量轉換爲字符串?](http://stackoverflow.com/questions/5093460/how-to-convert-an-enum-type-variable-to-a-string) – rsp