求高手帮忙挑下c++程序错误,程序如下 error C2593: 'operator >>。顺便想知道错在哪?我一定高分录用

#include<iostream>
using namespace std;
class fenshu{
public:
fenshu(){son=0;mom=0;} //构造函数
fenshu(double s,double m){son=s;mom=m;}

fenshu operator +(fenshu &f2); //运算符重载
fenshu operator -(fenshu &f2);
fenshu operator *(fenshu &f3);
fenshu operator /(fenshu &f4);

friend ostream& operator <<(ostream&,fenshu&); //对<< >>重载
friend istream& operator >>(istream&,fenshu&);

double yuefen(double,double); //约分函数
double guifan(double,double); //规范化函数
public:
double son;
double mom;
};

fenshu fenshu::operator + (fenshu &f2) //+重载
{return fenshu(son*f2.mom+f2.son*mom,mom*f2.mom);}
fenshu fenshu::operator - (fenshu &f2) //-重载
{return fenshu(son*f2.mom-f2.son*mom,mom*f2.mom);}
fenshu fenshu::operator * (fenshu &f2) //×重载
{return fenshu(son*f2.son,mom*f2.mom);}
fenshu fenshu::operator / (fenshu &f2) //除
{return fenshu(son*f2.mom,mom*f2.son);}

double fenshu::yuefen(double,double){
double a,b,t;
a=son;
b=mom;
do{
if(a>b)
{t=a-b;
a=t;};
if(b>a)
{t=b-a;
t=b;};
}while(a=b);
return(son/a,mom/b);
}
double fenshu::guifan(double,double){
double q,w;
q=son,w=mom;
if(q>0,w<0||q<0,w<0)
{q=-q;
w=-w;}
return(q,w);}

ostream& operator <<(ostream& output,fenshu&f)
{output<<"("<<f.son<<"/"<<f.mom<<")";
return output;}
istream& operator >>(istream& input,fenshu&f)
{cout<<"请输入分数的分子分母:";
input>>f.son>>f.mom;
return input;
}
int main(){
fenshu f1,f2,f3;
int x;
cin>>f1>>f2;
cout<<"您需要进行的操作是(1+,2-,3*,4/)"<<endl;
cin>>x;
if(x=1)
{ f3=f1+f2;
f3.yuefen;
f3.guifan;
}
if(x=2)
{ f3=f1-f2;
f3.yuefen;
f3.guifan;
}
if(x=3)
{ f3=f1*f2;
f3.yuefen;
f3.guifan;
}
if(x=4)
{ f3=f1/f2;
f3.yuefen;
f3.guifan;}
cout<<f3;

return 0;
}

第1个回答  2012-08-14
你再试试 ===>在重载operrator<<和>>前加上std::
或者#ingclude<iostream> using namespace std::改为#ingclude<iostream.h>追问

std:: 这个怎么加?在哪?

std:: friend ostream& operator <<(ostream&,fenshu&);是这样写吗?

追答

在实现的时候。 std:: ostream& operator using namespace std::改为#ingclude

第2个回答  推荐于2016-09-13
#include<iostream>
using namespace std;
class fenshu
{
public:
fenshu(){ son = 0 ; mom = 0 ; } //构造函数
fenshu( double s , double m ) { son= s ; mom = m ; }

fenshu operator +(fenshu &f2); //运算符重载
fenshu operator -(fenshu &f2);
fenshu operator *(fenshu &f3);
fenshu operator /(fenshu &f4);

friend ostream& operator <<(ostream&,fenshu&); //对<< >>重载
friend istream& operator >>(istream&,fenshu&);

fenshu yuefen(); //约分函数
fenshu guifan(); //规范化函数
private://私有数据成员
double son;
double mom;
};

fenshu fenshu::operator + (fenshu &f2) //+重载
{return fenshu(son*f2.mom+f2.son*mom,mom*f2.mom);}
fenshu fenshu::operator - (fenshu &f2) //-重载
{return fenshu(son*f2.mom-f2.son*mom,mom*f2.mom);}
fenshu fenshu::operator * (fenshu &f2) //×重载
{return fenshu(son*f2.son,mom*f2.mom);}
fenshu fenshu::operator / (fenshu &f2) //除
{return fenshu(son*f2.mom,mom*f2.son);}
fenshu fenshu::yuefen()//这里double应该为fenshu类型的而不是double类型的并且不写形参
{
double a , b , t ;
a = son ;
b = mom ;
do{
if(a>b)
{t=a-b;
a=t;};
if(b>a)
{t=b-a;
t=b;};
}while(a==b);//应该改为==
return fenshu(son/a,mom/b);//相应的改为
}
fenshu fenshu::guifan()//这里double也应该为fenshu类型的而不是double类型的
{
double q,w;
q=son,w=mom;
if((q>0&&w<0)||(q<0&&w<0))//这句话语法有问题
{q=-q;
w=-w;}
return fenshu (q,w);//相应的改为fenshu (q,w)
}

ostream& operator <<(ostream& output,fenshu&f)
{output<<"("<<f.son<<"/"<<f.mom<<")";
return output;}
istream& operator >>(istream& input,fenshu&f)
{cout<<"请输入分数的分子分母:";
input>>f.son>>f.mom;
return input;
}
int main(){
fenshu f1,f2,f3;
int x;
cin>>f1>>f2;
cout<<"您需要进行的操作是(1+,2-,3*,4/)"<<endl;
cin>>x;
if(x==1)//应用==
{ f3=f1+f2;
f3.yuefen();//后面的括号不能少
f3.guifan();
}
if(x==2)//应用==
{ f3=f1-f2;
f3.yuefen();
f3.guifan();
}
if(x==3)
{ f3=f1*f2;
f3.yuefen();//同上
f3.guifan();
}
if(x==4)
{ f3=f1/f2;
f3.yuefen();
f3.guifan();}
cout<<f3;

return 0;
}追问

虽然好多了,但还是编译不行,还是有error C2593: 'operator >>。能帮忙解决下这个吗?

追答

我的编译器上没问题啊。。。你用的什么编译器??我看看

追问

vc6.0

追答

我用的minGw上面没有错误,,要不然远程下我看看

本回答被提问者和网友采纳

求高手帮忙挑下c++程序错误,程序如下 error C2593: 'operator >>。顺便...
你再试试 ===>在重载operrator<<和>>前加上std::或者#ingclude<iostream> using namespace std::改为#ingclude<iostream.h>

...is ambiguoe error C2593: 'operator >>' is ambiguous
class storehouse{ int mutex; int count; int max; void save(){ int changed=false; while(changed==false){ changed=true; enter(mutex); if(count<max)count++; else changed=false; leave(mutex); sleep(0); } } void sell(){ ...

error C2593: 'operator >>' is ambiguous 是什么原因啊vc6.0的_百 ...
操作符 >> 的定义不明确或有歧义 你看下是不是重载时出问题了

相似回答
大家正在搜