| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 789 人关注过本帖
标题:重載运算符问题,为啥有歧义
只看楼主 加入收藏
唐兵
Rank: 2
来 自:四川泸州合江
等 级:论坛游民
帖 子:150
专家分:12
注 册:2012-12-1
结帖率:82.05%
收藏
已结贴  问题点数:20 回复次数:5 
重載运算符问题,为啥有歧义
#include <iostream>
using namespace std;
class Point
{
public:
    Point(int a,int b);
    friend ostream & operator<<(ostream &output,Point &p );
    void getxy();
public:
    int  x,y;
};
Point::Point(int a,int b)
    {
        x=a;
        y=b;
    }
void Point::getxy()
{
    cout<<"input x,y:"<<endl;
    cin>>x>>y;
}
class Circle:public Point
{
public:
    friend ostream & operator<<(ostream &output,Point &p );
    Circle(int a,int b,int r1);
    int area();
    void getr();
    int r;
};
Circle::Circle(int a,int b,int r1):Point(a,b),r(r1){}
void Circle::getr()
{
    cout<<"inpu r:"<<endl;
    cin>>r;
}
int Circle::area()
{
    return 3.14*r*r;
}
class Cyinder:public Circle
{
public:
    int h;
};
ostream & operator<<(ostream &output,Circle &C )
{
    output<<C.x<<endl<<C.y<<endl<<C.area()<<endl;
    return output;
}
ostream & operator<<(ostream &output,Point &p )
{
    output<<p.x<<endl<<p.y<<endl;
    return output;
}
int main()
{
    Point o(1,2);
    o.getxy();
    cout<<o;
    Circle c(1,2,3);
    c.getr();
    cout<<c;
    return 0;                  
}
搜索更多相关主题的帖子: public namespace void include friend 
2013-06-04 18:00
唐兵
Rank: 2
来 自:四川泸州合江
等 级:论坛游民
帖 子:150
专家分:12
注 册:2012-12-1
收藏
得分:0 
ostream & operator<<(ostream &output,Circle &C )
重载有歧义

一个人如果不比自己一把,你永远不知道自己究竟有多厉害
2013-06-05 13:41
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
ostream & operator<<(ostream &output,Circle &C )
{
    output<<C.x<<endl<<C.y<<endl<<C.area()<<endl;
    return output;
}
ostream & operator<<(ostream &output,Point &p )
{
    output<<p.x<<endl<<p.y<<endl;
    return output;
}

函数参数个数和类型一样 和 返回函数类型一致你重新写 一个输出函数把
因为 Point 和Circle  你把& 去掉时下
2013-06-05 15:47
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:7 
回复 3楼 haoyasen
你用什么编译器?
2013-06-05 16:14
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:7 
回复 2楼 唐兵
我运行你的代码 一个错误也没啊怎么我用的是VS 2012
2013-06-05 21:09
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
回复 4楼 rjsp
VS 2012
2013-06-05 21:09
快速回复:重載运算符问题,为啥有歧义
数据加载中...
 
   



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

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