我是一名C++初学者,试图从在线视频中学习。在讲座中的操作符超载示例中,存在以下代码并给出了错误<<运算符在C++中的重载错误
error: no match for 'operator<<' in 'std::cout << operator+(((point&)(& p1)), ((point&)(& p2)))'compilation terminated due to -Wfatal-errors.
上标有评论的行。有人可以告诉代码中有什么问题吗?我只是在尝试教授在讲座中解释但无法编译的内容。
===============
#include <iostream>
using namespace std;
class point{
public:
double x,y;
};
point operator+ (point& p1, point& p2)
{
point sum = {p1.x + p2.x, p1.y + p2.y};
return sum;
}
ostream& operator<< (ostream& out, point& p)
{
out << "("<<p.x<<","<<p.y<<")";
return out;
}
int main(int argc, const char * argv[])
{
point p1 = {2,3};
point p2 = {2,3};
point p3;
cout << p1 << p2;
cout << p1+p2; // gives a compliation error
return 0;
}
谢谢大家。这工作。 – kaushal