这个重载(operator<<)为什么不行?

alexhilton alexhilton 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
重新学习C++中。
遇到了问题:
重载<<时总编译有错:

/*
* C plus plus learning.
* Question: How to overloading << for a class/object.
*/
#include <iostream>
using namespace std;

class Point {
private:
int x;
int y;
public:
Point();
Point( int x, int y );
~Point();
// FIXME: this overloading does not work
ostream &operator<<(ostream &output, const Point &thiz);
};

Point::Point() {}
Point::Point(int ax, int ay) {
x = ax;
y = ay;
}
Point::~Point() {}

ostream &Point::operator<<(ostream &output, const Point &thiz) {
cout << "(" << thiz.x << ", " << thiz.y << ")" << endl;
}

int main() {
Point a( 3, 4 );
cout << a;
return 0;
}

以上代码编译有错:
overload.cpp:17: error: ‘std::ostream& Point::operator<<(std::ostream&, const Point&)’ must take exactly one argument
overload.cpp:27: error: ‘std::ostream& Point::operator<<(std::ostream&, const Point&)’ must take exactly one argument
不知道是为什么,哪位大侠能帮忙分析下?
...全文

相关推荐

评论 18
收藏 已收藏
分享
返回顶部
alexhilton
C++ 语言 65,214 成员 250,514 内容