出现函数未编译错误
#include<iostream.h>
#include<cstdlib>
#include<cmath>
class Rational
{
public:
Rational(int a_son,int a_mother);
Rational(int whole_number);
Rational();
friend bool operator == (const Rational& number1,const Rational& number2);
friend bool operator < ( Rational& number1, Rational& number2);
friend bool operator <= ( Rational& number1, Rational& number2);
friend bool operator > ( Rational& number1, Rational& number2);
friend bool operator >= ( Rational& number1, Rational& number2);
friend Rational operator + (const Rational& number1,const Rational& number2);
friend Rational operator - (const Rational& number1,const Rational& number2);
friend Rational operator * (const Rational& number1,const Rational& number2);
friend Rational operator / (const Rational& number1,const Rational& number2);
friend ostream& operator<<(ostream& outs,const Rational& number);
friend istream& operator>>(istream& ins,Rational& number);
int Fun1(int a,int b); //求两个数的最大公约数
int Fun2(int a,int b); //求两个数的最小公倍数
private:
int son;
int mother;
};
int main()
{
Rational number1(1,2);
cout<<number1;
return 0;
}
Rational::Rational(int a_son,int a_mother)
{
if(a_son*a_mother>0)
{
son=abs(a_son);
mother=abs(a_mother);
}
else if(a_mother==0)
{
cout<<"you are wrong \n";
exit(1);
}
else
{
if(a_son>0)
{
son=-a_son;
}
else
{
son=a_son;
}
mother=abs(a_mother);
}
}
Rational::Rational(int whole_number)
{
son=whole_number;
mother=1;
}
Rational::Rational()
{
son=0;
mother=1;
}
int Rational::Fun1(int a,int b)
{
int count,i;
if(a<b)
{
i=a;
a=b;
b=i;
}
count=a%b;
if(count==0)
{
return b;
}
else
{
a=b;
b=count;
return Fun1(a,b);
}
}
int Rational::Fun2(int a,int b)
{
return a*b/Fun1(a,b);
}
bool operator==(const Rational& number1,const Rational& number2)
{
if(number1.son==number2.son && number1.mother==number2.mother)
{
return true;
}
else
{
return false;
}
}
bool operator < ( Rational& number1, Rational& number2)
{
if(static_cast<double>(number1.son)/number1.mother<static_cast<double>(number2.son)/number2.mother)
{
return true;
}
else
{
return false;
}
}
bool operator <= ( Rational& number1, Rational& number2)
{
if(static_cast<double>(number1.son)/number1.mother <=static_cast<double>(number2.son)/number2.mother)
{
return true;
}
else
{
return false;
}
}
bool operator > ( Rational& number1, Rational& number2)
{
if(static_cast<double>(number1.son)/number1.mother > static_cast<double>(number2.son)/number2.mother)
{
return true;
}
else
{
return false;
}
}
bool operator >= ( Rational& number1, Rational& number2)
{
if(static_cast<double>(number1.son)/number1.mother >= static_cast<double>(number2.son)/number2.mother)
{
return true;
}
else
{
return false;
}
}
Rational operator + (const Rational& number1,const Rational& number2)
{
Rational number;
int a;
int b;
int c;
a=Fun2(number1.mother,number2.mother);
b=number1.son*a/number1.mother+number2.son*a/number2.mother;
c=Fun1(a,abs(b));
number.son=b/c;
number.mother=a/c;
return number;
}
Rational operator - (const Rational& number1,const Rational& number2)
{
Rational number;
int a;
int b;
int c;
a=Fun2(number1.mother,number2.mother);
b=number1.son*a/number1.mother-number2.son*a/number2.mother;
c=Fun1(a,abs(b));
number.son=b/c;
number.mother=a/c;
return number;
}
Rational operator * (const Rational& number1,const Rational& number2)
{
Rational number;
int a;
int b;
b=number1.son*number2.son;
a=number1.mother*number2.mother;
number.son=a/Fun1(a,abs(b));
number.mother=b/Fun1(a,abs(b));
return number;
}
Rational operator / (const Rational& number1,const Rational& number2)
{
Rational number;
int a ;
int b;
b=number1.son*number2.mother;
a=number1.mother*number2.son;
number.mother=abs(a)/Fun1(abs(a),abs(b));
if(a<0)
{
number.son=-b/Fun1(abs(a),abs(b));
}
else
{
number.son=b/Fun1(abs(a),abs(b));
}
return number;
}
ostream& operator<<(ostream& outs,const Rational& number)
{
outs<<number.son<<'/'<<number.mother;
return outs;
}
istream& operator>>(istream& ins,Rational& number)
{
char ans;
int a_son;
int a_mother;
cin>>a_son>>ans>>a_mother;
if(a_mother<0)
{
number.son=-a_son;
number.mother=abs(a_mother);
}
else
{
number.son=a_son;
number.mother=a_mother;
}
return ins;
}
这里有什么错误?求高手指点。
[解决办法]
楼主好像用的是VC6.如果是的话建议不要用这个编译器学C++了。会误人子弟的。还有就是你的Fun1,Fun2等函数,和类没有什么关系,为什么要把它们定义为类的成员函数??
- C/C++ code
class Rational { public: Rational(int a_son,int a_mother); Rational(int whole_number); Rational(); friend bool operator == (const Rational& number1,const Rational& number2); friend bool operator < ( Rational& number1, Rational& number2); friend bool operator <= ( Rational& number1, Rational& number2); friend bool operator > ( Rational& number1, Rational& number2); friend bool operator >= ( Rational& number1, Rational& number2); friend Rational operator + (const Rational& number1,const Rational& number2); friend Rational operator - (const Rational& number1,const Rational& number2); friend Rational operator * (const Rational& number1,const Rational& number2); friend Rational operator / (const Rational& number1,const Rational& number2); friend ostream& operator<<(ostream& outs,const Rational& number); friend istream& operator>>(istream& ins,Rational& number); friend int Fun1(int a,int b); //求两个数的最大公约数 friend int Fun2(int a,int b); //求两个数的最小公倍数 private: int son; int mother; }; int main() { Rational number1(1,2); cout<<number1; return 0; } Rational::Rational(int a_son,int a_mother) { if(a_son*a_mother>0) { son=abs(a_son); mother=abs(a_mother); } else if(a_mother==0) { cout<<"you are wrong \n"; exit(1); } else { if(a_son>0) { son=-a_son; } else { son=a_son; } mother=abs(a_mother); } } Rational::Rational(int whole_number) { son=whole_number; mother=1; } Rational::Rational() { son=0; mother=1; } int Fun1(int a,int b) { int count,i; if(a<b) { i=a; a=b; b=i; } count=a%b; if(count==0) { return b; } else { a=b; b=count; return Fun1(a,b); } } int Fun2(int a,int b) { return a*b/Fun1(a,b); } bool operator==(const Rational& number1,const Rational& number2) { if(number1.son==number2.son && number1.mother==number2.mother) { return true; } else { return false; } } bool operator < ( Rational& number1, Rational& number2) { if(static_cast<double>(number1.son)/number1.mother<static_cast<double>(number2.son)/number2.mother) { return true; } else { return false; } } bool operator <= ( Rational& number1, Rational& number2) { if(static_cast<double>(number1.son)/number1.mother <=static_cast<double>(number2.son)/number2.mother) { return true; } else { return false; } } bool operator > ( Rational& number1, Rational& number2) { if(static_cast<double>(number1.son)/number1.mother > static_cast<double>(number2.son)/number2.mother) { return true; } else { return false; } } bool operator >= ( Rational& number1, Rational& number2) { if(static_cast<double>(number1.son)/number1.mother >= static_cast<double>(number2.son)/number2.mother) { return true; } else { return false; } } Rational operator + (const Rational& number1,const Rational& number2) { Rational number; int a; int b; int c; a=Fun2(number1.mother,number2.mother); b=number1.son*a/number1.mother+number2.son*a/number2.mother; c=Fun1(a,abs(b)); number.son=b/c; number.mother=a/c; return number; } Rational operator - (const Rational& number1,const Rational& number2) { Rational number; int a; int b; int c; a=Fun2(number1.mother,number2.mother); b=number1.son*a/number1.mother-number2.son*a/number2.mother; c=Fun1(a,abs(b)); number.son=b/c; number.mother=a/c; return number; } Rational operator * (const Rational& number1,const Rational& number2) { Rational number; int a; int b; b=number1.son*number2.son; a=number1.mother*number2.mother; number.son=a/Fun1(a,abs(b)); number.mother=b/Fun1(a,abs(b)); return number; } Rational operator / (const Rational& number1,const Rational& number2) { Rational number; int a ; int b; b=number1.son*number2.mother; a=number1.mother*number2.son; number.mother=abs(a)/Fun1(abs(a),abs(b)); if(a<0) { number.son=-b/Fun1(abs(a),abs(b)); } else { number.son=b/Fun1(abs(a),abs(b)); } return number; } ostream& operator<<(ostream& outs,const Rational& number) { outs<<number.son<<'/'<<number.mother; return outs; } istream& operator>>(istream& ins,Rational& number) { char ans; int a_son; int a_mother; cin>>a_son>>ans>>a_mother; if(a_mother<0) { number.son=-a_son; number.mother=abs(a_mother); } else { number.son=a_son; number.mother=a_mother; } return ins; }
[解决办法]
Fun1 Fun2是Rational的成员函数...你直接在外部函数中调用.....
要么设置成static,要么将函数写到类的外部