运算符重载有关问题,帮忙指点一下

运算符重载问题,各位高手帮忙指点一下
本帖最后由 VisualEleven 于 2014-04-11 13:03:04 编辑
题目:1. 定义一个有理数类,重载比较运算符.写一个完整的程序,进行数据成员的设置和输出。
class rational
{private:
 long denom,den;
 //denom为分子,den为分母
public:
rational(int num=0, int denom=1;
int operator<(rational r) const;
int operator<=(rational r) const;
int operator= =(rational r) const;
int operator!=(rational r) const;
//这里增加赋值和读出函数
}

.2.我编写的程序:
#include<iostream.h>
class rational
{
private:
long denom,den;
public:
rational(int num=0,int denom=1);
int operator<(rational r) const;
int operator<=(rational ) const;
int operator==(rational r) const;
int operator!=(rational ) const;
void getadd()
{
cout<<"enter the denom and den:"<<endl;
cin>>denom>>den;

}
void showdd()
{cout<<"denom="<<denom<<endl;
 cout<<"den="<<den<<endl;
}
};
rational::rational(int num,int denom)
{
this->den=num;
this->denom=denom;
}
int rational::operator<(rational r) const 
{
double f1=denom/den;
double f2=denom/den;
return (f1<f2)?1:0;
}
int rational::operator<=(rational r) const 
{
double f1=denom/den;
double f2=denom/den;
return (f1<=f2)?1:0;
}
int rational ::operator==(rational r) const
{
double f1=denom/den;
double f2=r.denom/r.den;
return (f1==f2)?1:0;
}
int operator::operator!=(rational r) const
{
double f1=denom/den;
double f2=r.denom/r.den;
return (f1!=f2)?1:0;

}
void main()
{
rational r1;
r1.getadd();
r1.showdd();
rational r2(3,5);
if(r1<r2)
cout<<"r1 is less than r2!"<<endl;
else if(r1<=r2)

cout<<"r1 is less than or equal to r2!"<<endl;
else if(r1==r2)
cout<<"r1 is equal to r2!"<<endl;
else
cout<<"r1 is not equal to r2!"<<endl;



}

3.调试不对:


Compiling...
2.cpp
H:\GIS\实验2\2\2\2.cpp(46) : error C2039: '!=' : is not a member of '`global namespace''
H:\GIS\实验2\2\2\2.cpp(46) : error C2833: 'operator !=' is not a recognized operator or type
H:\GIS\实验2\2\2\2.cpp(46) : warning C4091: '' : ignored on left of 'int' when no variable is declared
H:\GIS\实验2\2\2\2.cpp(46) : error C2143: syntax error : missing ';' before 'newline'
H:\GIS\实验2\2\2\2.cpp(46) : error C2143: syntax error : missing ';' before 'newline'
H:\GIS\实验2\2\2\2.cpp(47) : error C2143: syntax error : missing ';' before '{'
H:\GIS\实验2\2\2\2.cpp(47) : error C2447: missing function header (old-style formal list?)
执行 cl.exe 时出错.

2.obj - 1 error(s), 0 warning(s)
4.各位高手帮忙指导一下,错误在哪?应当如何写?它说的错误我改不出来,麻烦了哈
------解决方案--------------------
自己找问题吧,改好的如下:


#include<iostream>
using namespace std; 
class rational
{
private:
long denom,den;
public:
rational(int num=0,int denom=1);
int operator<(rational r) const;
int operator<=(rational ) const;
int operator==(rational r) const;
int operator!=(rational ) const;
void getadd()
{
cout<<"enter the denom and den:"<<endl;
cin>>denom>>den;

}
void showdd()
{cout<<"denom="<<denom<<endl;
 cout<<"den="<<den<<endl;
}
};
rational::rational(int num,int denom)
{
this->den=num;
this->denom=denom;
}
int rational::operator<(rational r) const 
{
double f1=denom/den;
double f2=denom/den;
return (f1<f2)?1:0;
}
int rational::operator<=(rational r) const 
{
double f1=denom/den;
double f2=denom/den;
return (f1<=f2)?1:0;
}
int rational::operator==(rational r) const
{
double f1=denom/den;
double f2=r.denom/r.den;
return (f1==f2)?1:0;
}
int rational::operator!=(rational r) const
{
double f1=denom/den;
double f2=r.denom/r.den;
return (f1!=f2)?1:0;

}
int main()
{
rational r1;
r1.getadd();
r1.showdd();
rational r2(3,5);
if(r1<r2)
cout<<"r1 is less than r2!"<<endl;
else if(r1<=r2)

cout<<"r1 is less than or equal to r2!"<<endl;