注册 登录
编程论坛 C++教室

此题错在哪里?

louchenggang 发布于 2017-10-04 15:14, 1467 次点击
#include <iostream>
using namespace std;
class Point {
    private:
        int x;
        int y;
    public:
        Point() { };
、/*friend ostream & operator << (ostream & o,const Point & s);
    friend istream & operator >> (istream & is,const Point & s);
     
    ostream & operator <<(ostream & o,const Point & s)
    {
         o<<s.x<<s.y;
        return o;
    }
     
    istream & operator >> (istream & is,const Point & s)
    {
         is >>s.x>>s.y;
        return is;
    }
    注释部分是我修改部分,您看错在哪里?
     */
};
int main()
{
     Point p;
     while(cin >> p) {
         cout << p << endl;
    }
    return 0;
}
错误提示说 ostream & operator <<(ostream & o,const Point & s)只能有一个参数?
2 回复
#2
yangfrancis2017-10-07 11:20
把o放在重载<<的函数体内定义,不作为传参
#3
rjsp2017-10-09 08:49
程序代码:
#include <iostream>

class Point
{
public:
    Point() : x_(), y_()
    {
    }
    Point( int x, int y ) : x_(x), y_(y)
    {
    }

    friend std::ostream& operator<< ( std::ostream& os, const Point& pt )
    {
        return os << pt.x_ << ' ' << pt.y_;
    }
    friend std::istream& operator>> ( std::istream& is, Point& pt )
    {
        return is >> pt.x_ >> pt.y_;
    }
private:
    int x_, y_;
};

using namespace std;

int main( void )
{
    for( Point pt; cin>>pt; )
    {
        cout << pt << endl;
    }
    return 0;
}
1