这个重载(operator<<)为什么不行?
重新学习C++中。
遇到了问题:
重载<<时总编译有错:
以上代码编译有错:
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
不知道是为什么,哪位大侠能帮忙分析下?
遇到了问题:
重载<<时总编译有错:
/*
* 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
不知道是为什么,哪位大侠能帮忙分析下?
...全文
相关推荐
C++ 流插入和流提取运算符的重载的实现
主要介绍了C++ 流插入和流提取运算符的重载的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
详解Python 函数如何重载?
主要介绍了Python函数重载,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
C++输入输出操作符重载的深入分析
本篇文章是对C++输入输出操作符重载进行了详细的分析介绍,需要的朋友参考下
C++为什么需要重载函数
C++为什么要重载函数,以及如何实现重载函数机制。
java方法重载示例
方法重载是以统一的方式处理不同数据类型的一种手段,这篇文章主要介绍了java方法重载示例,需要的朋友可以参考下


