【发布时间】:2019-10-14 04:24:23
【问题描述】:
我创建了“Point”类,在 2 Point 对象和运算符“
class Point {
public:
Point(int x=0, int y=0) : _x(x), _y(y) {};
Point operator +(Point &p);
int getX() {
return _x;
}
int getY() {
return _y;
}
friend ostream& operator <<(ostream& out, Point &p);
private:
int _x, _y;
};
Point Point::operator +(Point &p) {
Point np(this->_x+p.getX(), this->_y+p.getY());
return np;
}
ostream& operator <<(ostream &out, Point &p) {
out << '(' << p._x << ',' << p._y << ')';
return out;
}
int main() {
Point p1(1, 2);
Point p2;
cout << "p1: " << p1 << endl;
cout << "p2: " << p2 << endl;
cout << "p3: " << (p1+p2) << endl;
system("pause");
return 0;
}
【问题讨论】:
-
错误是什么?
-
没有匹配的运算符“
-
出现在这个"cout
-
不要评论。编辑您的问题并添加确切的错误消息。
-
FULL 错误,请。这可能是由于您的班级忽略了 const-correctness。
标签: c++