打印

操作符重载问题

[复制链接]
810|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
txcy|  楼主 | 2012-6-11 20:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<iostream>
using namespace std;

class Fraction
{

public:
double x,y;
Fraction(double a,double b)
{
x=a;
if(b==0)
{
cout<<"The denominator is 0."<<" "<<"Enter a new value: ";
cin>>y;
}
else
y=b;
}

~Fraction(){cout<<"End"<<endl;}

friend int operator >(Fraction m,Fraction n);

friend int operator <(Fraction m,Fraction n);


};

int operator >(Fraction m,Fraction n)
{
if((m.x/m.y)>(n.x/n.y))
return 1;
else
return 0;
}


int operator <(Fraction m,Fraction n)
{
if((m.x/m.y)<(n.x/n.y))
return 1;
else
return 0;
}

int main()
{
Fraction a(2.5,6.1),b(3.33,0);
cout<<(a>b)<<endl;
cout<<(a<b)<<endl;
return 0;
}

编译后指错:cout<<(a>b)<<endl;cout<<(a<b)<<endl;????

相关帖子

沙发
火箭球迷| | 2012-6-11 21:00 | 只看该作者
因是使用VC6,由于VC6存在bug,编译会出现内部错误,原因是不能在声明重载运算符之前写using namespace std
可以把<iostream>改为<iostream.h>删除using namespce std;

使用特权

评论回复
板凳
baidudz| | 2012-6-11 22:18 | 只看该作者
楼主什么编译器呢?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

274

主题

2106

帖子

0

粉丝