标题:关于友元重载的问题很简单的但老是编译有错!!
只看楼主
lzywin
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2009-11-11
结帖率:50%
已结贴  问题点数:20 回复次数:5 
关于友元重载的问题很简单的但老是编译有错!!
//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 编辑 ]
搜索更多相关主题的帖子: 重载 编译 
2009-12-01 20:58
hoho568
Rank: 5Rank: 5
等 级:职业侠客
帖 子:101
专家分:378
注 册:2009-7-14
得分:6 
你的问题不是重载,而是你的构造函数。。
2009-12-01 21:32
lzywin
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2009-11-11
得分:0 
哪里错了?
这个类的定义是给好的 只要求写成员函数的实现跟主函数
2009-12-01 21:37
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
得分:6 
程序代码:
istream& operator>> (istream& in,Point& p1)
{
in>>p1.x>>p1.y;
return in;
}


你能学会你想学会的任何东西,这不是你能不能学会的问题,而是你想不想学的问题
2009-12-01 22:09
lzywin
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2009-11-11
得分:0 
那个分号加上还是编译有误啊
2009-12-01 22:37
qlc00
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:157
专家分:540
注 册:2007-11-26
得分:6 
#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;


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

Anything is possible!
2009-12-02 15:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-293613-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.329743 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved