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

关于友元重载的问题很简单的但老是编译有错!!

lzywin 发布于 2009-12-01 20:58, 675 次点击
//Point类的定义
#ifndef POINT_H
#define POINT_H
#include<iostream.h>
class Point{
public:
    Point(int a=0,int b=0);
    Point(Point&);
    ~Point();
    int get_x()const;
    int get_y()const;
    void put_x(int a);
    void put_y(int b);
    Point operator=(const Point& p1);
    Point operator+(const Point& p1);
    Point operator*(int i);
private:
    int x,y;
    friend istream& operator>>(istream&  in,Point& p1);
                            //重载提取操作符函数(输入),友元函数
    friend ostream& operator<<(ostream& out,Point& p1);
                            //重载插入操作符函数(输出),友元函数
};
#endif
//此处是成员函数的实现。
ostream&  operator <<(ostream& out,Point& p1)
{     out <<"输入2个数"<<"("<<p1.x<<p1.y<<")"<<endl;
return out;
}
istream& operator>> (istream& in,Point& p1)
{

in>>p1.x>>p1.y;
return in
}
//测试成员函数
#include"point.h"

void main()
{
    Point p2(1,2),p1;

cout<<p2<<p1;


}



[ 本帖最后由 lzywin 于 2009-12-1 21:42 编辑 ]
5 回复
#2
hoho5682009-12-01 21:32
你的问题不是重载,而是你的构造函数。。
#3
lzywin2009-12-01 21:37
哪里错了?
这个类的定义是给好的 只要求写成员函数的实现跟主函数
#4
flyingcloude2009-12-01 22:09
程序代码:
istream& operator>> (istream& in,Point& p1)
{
in>>p1.x>>p1.y;
return in;
}

#5
lzywin2009-12-01 22:37
那个分号加上还是编译有误啊
#6
qlc002009-12-02 15:29
#ifndef POINT_H
#define POINT_H
#include<iostream.h>
class Point
{
public:
    Point(int a=0,int b=0)
    {
        x=a;
        y=b;
    }
    ~Point(){}
    int get_x()const;
    int get_y()const;
    void put_x(int a);
    void put_y(int b);
    Point operator=(const Point& p1);
    Point operator+(const Point& p1);
    Point operator*(int i);
private:
    int x,y;
    friend istream& operator>>(istream& in,Point& p1);
                            //重载提取操作符函数(输入),友元函数
    friend ostream& operator<<(ostream& out,Point& p1);
                            //重载插入操作符函数(输出),友元函数
};
#endif
//此处是成员函数的实现。
ostream&  operator <<(ostream& out,Point& p1)
{     
    out <<"输入2个数"<<"("<<p1.x<<p1.y<<")"<<endl;
return out;
}
istream& operator>> (istream& in,Point& p1)
{

in>>p1.x>>p1.y;
return in;
}
//测试成员函数
void main()
{
    Point p2(1,2),p1;

cout<<p2<<p1;


}
这个调试后没有错误,你虚函数只声明而没有实现。
1