【发布时间】:2010-05-13 17:09:16
【问题描述】:
我用这个问题编辑了我的帖子,但没有得到任何答案。
我在 score.cpp 中为类Score(在 score.h 中定义)重载了
ostream& operator<< (ostream & os, const Score & right)
{
os << right.getPoints() << " " << right.scoreGetName();
return os;
}
(getPoints 获取一个int 属性,getName 一个string 一个)
我在 main() 中的测试中收到此编译错误,包含在 main.cpp 中
binary '<<' : no operator found which takes a right-hand operand of type 'Score' (or there is no acceptable conversion)
为什么编译器不“识别”该重载是有效的? (包括是正确的)
感谢您的宝贵时间。
编辑:
根据要求,导致错误的代码:
cout << ":::::\n" << jogador.getScore() << endl;
jogador 是一个Player 对象,其中包含一个Score。 getScore 返回该属性。
【问题讨论】:
-
您绝对应该发布导致错误的代码
标签: c++ class operator-overloading