这个重载(operator<<)为什么不行?-CSDN社区 (2025)

社区 C++ 语言

帖子详情

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
不知道是为什么,哪位大侠能帮忙分析下?

...全文

2603 18

打赏 收藏

分享

转发到动态

举报

AI 作业

写回复

用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]
正解
用重载《和》必须用友元来实现

生活简单到无聊 2010-09-11

  • 打赏
  • 举报
回复

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;

高性能架构探索 2010-09-10

  • 打赏
  • 举报
回复

记得 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

  • 打赏
  • 举报
回复

看来楼主现在用的教材还是太差了嘛。

这个重载(operator<<)为什么不行?-CSDN社区 (20) C++中operator<<运算符重载注意事项

我们在定义一个类的时候,为了使用方便,希望通过<<打印类的相关信息,需要重载<<运算符一般运算符重载都设计为类的member function,但是operator<<却不能这样设计,Essential C++中解释如下: 为什么不把output运算符设计为一个member function呢?因为作为一个member function,其左侧操作数必须是隶属同一个class之下的对象,如果ou

这个重载(operator<<)为什么不行?-CSDN社区 (21) C++模板类;友元重载operator<<,使其也具有泛型特性; 非成员重载运算符的模板。

目录前言实例效果参考前言实现的模板类中,如果要打印输出实时的私有成员内容,重载operator<<就可并设为友元即可。operator<<也需要泛型,就将其也做成模板函数,再在类外实现即可。实例代码如下(示例):用栈实现一个队列,队列是私有的。这个类是模板类。通过设定友元,使得(非成员重载运算符operator<<)能够访问到对象中私有成员。template<typename TT>class MyQueue {private:s

这个重载(operator<<)为什么不行?-CSDN社区 (22) priority_queue元素为结构体时需重载operator<

STL priority_queue具有“自动排序”的强大功能。其默认使用operator<的比较方式进行排序。但是,对于自定义类型(如结构体、联合体、枚举等),则必须重载operator<比较方式。重载 operator< 的一个示例代码如下所示:bool operator<(Node a,Node b){if(a.pr!=b.pr)return a.pr<b.pr;elsereturn a.index>b.index;}...

这个重载(operator<<)为什么不行?-CSDN社区 (23) 为什么operator<<左移运算符不能重载成员函数,而只能重载为友元函数?

关于运算符重载1.首先我们通过operator+加号运算符来了解成员函数与友元函数的区别。1.加号运算符的成员函数2.加号运算符的友元函数2.operator<<左移运算符不能重载成员函数,而只能重载为友元函数。1.左移运算符成员函数达不到我们的效果2.如果使用友元函数1.首先我们通过operator+加号运算符来了解成员函数与友元函数的区别。首先我们来看一下operator+加号运算符,因为他的重载继可实现成员函数重载,也可以实现友元函数重载。1.加号运算符的成员函数我们来看一下加号运

这个重载(operator<<)为什么不行?-CSDN社区 (24) 重载operator<<操作符—friend ostream& operator<<(ostream&,const Date&);

利用friend友元函数对操作符#include using namespace std; class Date{friend ostream& operator<<(ostream&,const Date&); int year; int month; int day;public: Date() {year=2013; month=

这个重载(operator<<)为什么不行?-CSDN社区 (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Jamar Nader

Last Updated:

Views: 5522

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.