本文介绍了我该如何清除错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在下面运行代码时,我收到'class fraction'没有名为'inc'的成员
我想以朋友的身份运行所有功能。
请建议
我尝试过:
When I run the code on below, I recieve 'class fraction' has no member named 'inc'
I want to run all functions as friend.
Please advice
What I have tried:
#include <iostream>
#include <string.h>
using namespace std;
class fraction
{
long num;
long den;
public:
fraction ();
fraction(long,long);
fraction(const fraction &);
~fraction();
long getnum (void)const;
long getDen (void)const;
void print (void);
friend fraction add (const fraction &, const fraction &);
friend fraction sub (const fraction &, const fraction &);
friend fraction mult(const fraction &, const fraction &);
friend fraction div (const fraction &, const fraction &);
friend fraction inc (const fraction &);
};
fraction::fraction(long l_num,long l_den)
{
num = l_num ;
den = l_den ;
}
fraction::fraction( )
{
num = 0L ;
den = 1L ;
}
fraction::fraction(const fraction& F)
{
num=F.num;
den=F.den;
}
fraction:: ~fraction ( )
{
// cout<<"Destructor"<<endl;
}
long fraction::getDen (void) const
{
return den ;
}
long fraction::getnum (void) const
{
return num ;
}
void fraction:: print (void)
{
if(den==0)
den=1L;
cout<<num<<"/"<<den <<endl;
}
fraction add( const fraction &f1, const fraction &f2)
{
long num = (f1.getnum ( ) * f2.getDen ( )) + ( f1.getDen ( ) * f2.getnum ( ) );
long den = (f1.getDen ( ) * f2.getDen ( ));
return fraction(num, den);
}
fraction sub ( const fraction &f1, const fraction &f2)
{
long num = (f1.getnum ( ) * f2.getDen ( )) - ( f1.getDen ( ) * f2.getnum ( ) );
long den = (f1.getDen ( ) * f2.getDen ( ));
return fraction(num, den);
}
fraction mult ( const fraction &f1, const fraction &f2)
{
long num = (f1.getnum ( ) * f2.getnum ( ));
long den = (f1.getDen ( ) * f2.getDen ( ));
return fraction(num, den);
}
fraction div( const fraction &f1, const fraction &f2)
{
long num = (f1.getnum ( ) * f2.getDen ( ));
long den = (f1.getDen ( ) * f2.getnum ( ));
return fraction(num, den);
}
fraction inc (const fraction &f1)
{
return add(f1,fraction(1,1));
}
int main()
{
fraction f1,f2(5L,0L);
fraction f3(f2);
f1.print();
f2.print();
f3.print();
f3 = add(f3 , fraction (-7,8) );
f1 = add(f2,f3);
f1.print ();
f1 = sub(f2 , f3);
f1.print();
f1 = mult(f2, f3);
f1.print();
f1.inc().inc().print();
f1=div(f2 , f3 );
f1.print();
return 0;
}
推荐答案
class fraction
{
// ...
fraction inc();
};
fraction fraction::inc()
{
*this = inc(*this);
return *this;
}
这篇关于我该如何清除错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!