程序如下:
#include
using namespace std;
class zrf_Ratio;
zrf_Ratio operator+(const zrf_Ratio& r1, const zrf_Ratio& r2);
zrf_Ratio operator-(const zrf_Ratio&, const zrf_Ratio&);
zrf_Ratio operator*(const zrf_Ratio&, const zrf_Ratio&);
zrf_Ratio operator/(const zrf_Ratio&, const zrf_Ratio&);
class zrf_Ratio
{
public:
zrf_Ratio(){}
zrf_Ratio(int r1,int r2):num(r1),den(r2){}
zrf_Ratio f1(zrf_Ratio& z)
{
int r,m=z.num,n=z.den;
while(r=z.num%z.den)
{
z.num=z.den;
z.den=r;
}
r=z.den;
z.num=m/r;
z.den=n/r;
return z;
}
friend ostream& operator<<(ostream& ostr, const zrf_Ratio& r)
{ return ostr << r.num << "/" << r.den;}
zrf_Ratio operator-(const zrf_Ratio& r1)
{return f1(zrf_Ratio(num*r1.den-r1.num*den,den*r1.den));}
friend zrf_Ratio operator+(const zrf_Ratio& r1, const zrf_Ratio& r2)
{
zrf_Ratio a;
return a.f1(zrf_Ratio(r1.num*r2.den+r2.num*r1.den,r1.den*r2.den));
}
friend zrf_Ratio operator-(const zrf_Ratio& r1, const zrf_Ratio& r2)
{
zrf_Ratio a;
return a.f1(zrf_Ratio(r1.num*r2.den-r2.num*r1.den,r1.den*r2.den));
}
friend zrf_Ratio operator*(const zrf_Ratio& r1, const zrf_Ratio& r2)
{
zrf_Ratio a;
return a.f1(zrf_Ratio(r1.num*r2.num,r1.den*r2.den));
}
friend zrf_Ratio operator/(const zrf_Ratio& r1, const zrf_Ratio& r2)
{
zrf_Ratio a;
return a.f1(zrf_Ratio(r1.num*r2.den,r1.den*r2.num));
}
private:
int num,den;
};
int mian()
{
zrf_Ratio x(3,7),y(4,9),z;
z=x.operator-(y);
cout<<"x-y="<<z<<endl;
z=x+y;
cout<<"x+y="<<z<<endl;
z=x-y;
cout<<"x-y="<<z<<endl;
z=x*y;
cout<<"x*y="<<z<<endl;
z=x/y;
cout<<"x/y="<<z<<endl;
return 0;
}
顯示錯誤如下:
Deleting intermediate files and output files for project '2 - Win32 Debug'.
--------------------Configuration: 2 - Win32 Debug--------------------
Compiling...
2.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/2.exe : fatal error LNK1120: 1 unresolved externals
執行 link.exe 時出錯.
2.exe - 1 error(s), 0 warning(s)
main拼寫錯了