我正在制作一个名为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