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

alexhilton 2010-09-10 11:30:32
重新学习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
不知道是为什么,哪位大侠能帮忙分析下?
...全文
2217 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
elegant87 2010-09-12
  • 打赏
  • 举报
回复 1
[Quote=引用 13 楼 cxxer 的回复:]

C/C++ code
/*
* C plus plus learning.
* Question: How to overloading << for a class/object.
*/
#include <iostream>
#include <cstdlib>

using namespace std;

class Point {
private:
int x;
……
[/Quote]
正解
用重载《和》必须用友元来实现
  • 打赏
  • 举报
回复
ostream &Point::operator<<(ostream &output, const Point &thiz);
把这个改成友元函数就OK
wo370506875 2010-09-11
  • 打赏
  • 举报
回复
类成员的函数重载,类对象是默认左操作的,所以你的调用应该是a<<(cout,a);
cumt_TTR 2010-09-11
  • 打赏
  • 举报
回复
要搞成友元函数才行吧
dingshaofengbinbin 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chain2012 的回复:]
C/C++ code
friend ostream &operator<<(ostream &output, const Point &thiz);


ostream &operator<<(ostream &output, const Point &thiz)
{
output << "(" << thiz.x <<……
[/Quote]
正解,看来LZ并没有搞清楚operator这个东西啊!!!!
pengzhixi 2010-09-10
  • 打赏
  • 举报
回复
加上 return output;
  • 打赏
  • 举报
回复
记得 return一下你的ostream
chainyu 2010-09-10
  • 打赏
  • 举报
回复
还有 << endl; 最好不带
hai040 2010-09-10
  • 打赏
  • 举报
回复
operator<<不能写成成员
要写也只能写能stream的成员
chainyu 2010-09-10
  • 打赏
  • 举报
回复
    friend ostream &operator<<(ostream &output, const Point &thiz);


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


这样就行了
huanok 2010-09-10
  • 打赏
  • 举报
回复
返回类型。。。
cxxer 2010-09-10
  • 打赏
  • 举报
回复
/*
* C plus plus learning.
* Question: How to overloading << for a class/object.
*/
#include <iostream>
#include <cstdlib>

using namespace std;

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

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

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

int main() {
Point a( 3, 4 );
cout << a << endl;
system("PAUSE");
return 0;
}
gules 2010-09-10
  • 打赏
  • 举报
回复 1
重载操作符必须符合内建的模型,也就是说,重载操作符后的行为要与原来一致。

<< 操作符是一个二元操作符,它只接受二个参数,调用时的形式为:第一个参数<<第二个参数,如:
ostream& operator<< (ostream& os, const Point& pt);
的调用形式为: os<<pt (返回引用是为了可以连写:os << pt1 << pt2; )

而如果要将<<操作符定义为类的成员函数,那么你就只能给一个参数(因为编译器展开类成员函数时将*this对象作为第一个参数),如果你给两个参数,那最后<<操作就变成三元的了,与原模型不符,所以编译器给出“overload.cpp:17: error: ‘std::ostream& Point::operator<<(std::ostream&, const Point&)’ must take exactly one argument”的错误。

如果你偏要定义成成员函数,并正确的只给出一个参数ostream&,那么最后的调用式就只能写成:

pt << std::cout;

那么你是不是觉得“违反了标准”呢(虽然程序可正确运行)?而且如果返回ostream&,就无法连写了!
liutengfeigo 2010-09-10
  • 打赏
  • 举报
回复
如果是树上的列子,确实该换本书。
c++ primer plus.
c++ primer
harderman 2010-09-10
  • 打赏
  • 举报
回复
当定义符合标准库iostream规范的输入或输出操作符的时候,必须使它成为非成员操作符
helloworldang 2010-09-10
  • 打赏
  • 举报
回复
ostream& operator<<(ostream& output,const A& object)如果为成员函数,
上面的代码相当于ostream& operator<<(ostream& output)
int main()
{
A a;
a << cout;
}而一般是要这样,cout << a也就变成最上面那样,然后因为需要输出或者读入该类的private成员或者protected成员,所以就把声明为友元,谁的友元?A的!B的呢?刚刚翻了翻C++ PRIMER,哈哈
friend& operator<<(ostream& output,const B& object)


[Quote=引用 8 楼 helloworldang 的回复:]
形参要一致

引用 6 楼 dingshaofengbinbin 的回复:
引用 1 楼 chain2012 的回复:
C/C++ code
friend ostream &amp;amp;operator<<(ostream &amp;amp;output, const Point &amp;amp;thiz);


ostream &amp;am……
[/Quote]
helloworldang 2010-09-10
  • 打赏
  • 举报
回复
形参要一致[Quote=引用 6 楼 dingshaofengbinbin 的回复:]
引用 1 楼 chain2012 的回复:
C/C++ code
friend ostream &amp;operator<<(ostream &amp;output, const Point &amp;thiz);


ostream &amp;operator<<(ostream &amp;output, const Point &am……
[/Quote]
taodm 2010-09-10
  • 打赏
  • 举报
回复
看来楼主现在用的教材还是太差了嘛。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧