我正在製作一個名爲Magick的基於文本的冒險遊戲。
在這場比賽中我有一個類標記damageSpell
它看起來像這樣當試圖打印向量元素時,'operator <<'不匹配
class damageSpell {
public:
int damage;
SubClasses type;
int manaCost;
std::string spellDescription;
};
我後來就用這個類作爲類型的載體,像這樣
std::vector<damageSpell> damageSpells
,我嘗試添加通過使用矢量上的insert
函數將一個元素插入到我的damageSpells
向量中。
damageSpell fireball;
user.damageSpells.insert(user.damageSpells.begin(), 0, fireball);
然後試圖將其打印出來
std::cout << user.damageSpells[0];
一旦這樣做,我收到此錯誤
magick1.cpp:252:15: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘damageSpell’)
我新的C++,而且不知道這意味着什麼或如何我應該去解決它,任何和所有的幫助將不勝感激。
[運算符重載](http://stackoverflow.com/questions/4421706/operator-overloading)將提供信息,特別是關於「Bitshift運算符」的選定答案部分。 – WhozCraig
使用'<<'運算符輸出不是自動發生的。相反,有'''運算符的特殊重載處理每個標準類型的輸出。如果你想用輸出操作符輸出你自己的結構,你需要編寫你自己定製的'operator <<'函數。幾乎所有[良好的初學者書](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)將教你如何做到這一點。 –
你認爲在矢量中放入東西會使它不可打印嗎?也許忘記矢量並嘗試打印'damageSpell'對象? – juanchopanza