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

帮帮忙,重载运算符相关的

hy247767221 发布于 2011-10-14 15:30, 744 次点击
#include<iostream>
using namespace std;

class Point
{
    public:
        Point() : x(0), y(0)
        {
            cout << "Initialize!" << endl;
        }
        Point(int a, int b) : x(a), y(b)
        {
            cout << "Initialize!" << endl;
        }
        friend ostream &operator << (ostream& output, Point& point);
        friend istream &operator > (istream& input, Point &point);
        ~Point()
        {
            cout << "Set free!" << endl;
        }
    private:
        int x, y;
};

ostream &operator << (ostream& output, Point& point)
{
    output << "(" << point.x << "," << point.y << ")" << endl;
    return output;
}
istream &operator>> (istream& input, Point &point)
{
    input >> point.x;
    input >> point.y;
    return input;
}


int main()
{
    Point A(3, 5), B;
    cin >> B;
    cout << A << endl << B << endl;

    return 0;
}
9 回复
#2
nomify2011-10-14 16:18
friend istream &operator > (istream& input, Point &point); // >>
#3
hy2477672212011-10-14 17:04
回复 2楼 nomify
好像不对啊 ,不是这样的,我用的是VC++编译的
#4
yy7software2011-10-14 19:19
程序代码:
#include<iostream>
using namespace std;

class Point
{
    public:
        Point() : x(0), y(0)
        {
            cout << "Initialize!" << endl;
        }
        Point(int a, int b) : x(a), y(b)
        {
            cout << "Initialize!" << endl;
        }
        friend ostream &operator << (ostream& output, Point& point)
    {
            output << "(" << point.x << "," << point.y << ")" << endl;
            return output;
    }   
        friend istream &operator >> (istream& input, Point &point)
    {
            input >> point.x;
            input >> point.y;
            return input;
    }
        ~Point()
        {
            cout << "Set free!" << endl;
        }
    private:
        int x, y;
};
int main()
{
    Point A(3, 5), B;
    cin >> B;
    cout << A << endl << B << endl;

    return 0;
}
#5
yy7software2011-10-14 19:21
这个就是对的,把定义格式放到类体内,,
#6
hy2477672212011-10-14 19:52
回复 5楼 yy7software
为什么要这样呢,不是友员函数么,放在外面为什么不行呢?
#7
niitzz2011-10-14 20:59
楼主把#include<iostrema> 改成#include<iostrema>    然后把using namespace std;这一行注释掉就可以通过了
#8
hy2477672212011-10-14 22:04
回复 7楼 niitzz
不是吧,有#include<iostrema> 这样的么?
是不是错了
应该是<iostream>的
#9
nomify2011-10-14 23:37
程序代码:
#include<iostream>
using namespace std;

class Point;
ostream &operator << (ostream& output, Point& point);
istream &operator>> (istream& input, Point &point);

class Point
{
    public:
        Point() : x(0), y(0)
        {
            cout << "Initialize!" << endl;
        }
        Point(int a, int b) : x(a), y(b)
        {
            cout << "Initialize!" << endl;
        }
        friend ostream &operator << (ostream& output, Point& point);
        friend istream &operator >> (istream& input, Point &point);
        ~Point()
        {
            cout << "Set free!" << endl;
        }
    private:
        int x, y;
};

ostream &operator << (ostream& output, Point& point)
{
    output << "(" << point.x << "," << point.y << ")" << endl;
    return output;
}
istream &operator>> (istream& input, Point &point)
{
    input >> point.x;
    input >> point.y;
    return input;
}


int main()
{
    Point A(3, 5), B;
    cin >> B;
    cout << A << endl << B << endl;

    return 0;
}
#10
lucky5635912011-10-15 08:17
错误信息干吗不贴出来
1