我是C++的初学者,我有以下问题。 当我在VS2013中运行以下代码时,出现错误。C++ operator <<重载
class Y{
public:
Y(int un_x, int un_y)
: x_(un_x), y_(un_y) {}
int x() const {
return x_;
}
int y() const {
return y_;
}
private:
int x_;
int y_;
};
class X{
private:
Y coord;
public:
// some code ...
Y position() const {
return coord;
}
void display(ostream& output) const {
output << "The object is in position " << position();
}
};
ostream& operator<<(ostream& output, Y x){
output<< "(" << x.x() << ", " << x.y() << ")" << endl;
return output;
}
如果我创建类X的对象some_object并尽量做到:
cout << some_object ;
我得到followng错误:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Y' (or there is no acceptable conversion)
您无法运行该代码,因为没有实际的代码可以运行(如主功能)。如果你的意思是你在代码下面提到的内容,那么输出'X'不会超载。 – chris
上面链接的代码由MooingDuck编译并在VS2013下正确运行。请发布重现问题的[SSCCE](http://sscce.org)。 – Praetorian
另外,'X'与这个问题有什么关系? –