我收到Id返回1退出状态错误。 inc函数不应该得到任何论证者。 我试图把它作为朋友功能这次我收到这个错误“inc不是成员功能”
请告知
#include <iostream>
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);
fraction inc();
friend fraction add(const fraction &f1, const fraction &f2);
friend fraction sub(const fraction &f1, const fraction &f2);
friend fraction mult(const fraction &f1, const fraction &f2);
friend fraction div(const fraction &f1, const fraction &f2);
};
fraction::fraction()
{
num = 0L;
den = 1L;
}
fraction::fraction(long l_num, long l_den)
{
num = l_num;
den = l_den;
}
fraction::fraction(const fraction& f)
{
num = f.num;
den = f.den;
}
fraction:: ~fraction()
{
cout << "Destructor" << endl;
}
long fraction::getNum(void) const
{
return num;
}
long fraction::getDen(void) const
{
return den;
}
void fraction::print(void)
{
if (den == 0)
den = 1L;
cout << num << "/" << den << endl;
}
fraction fraction::inc()
{
long num = (num)+(den);
long den = (den);
return fraction(num, den);
}
fraction add(fraction &f1, fraction &f2)
{
long num = (f1.getNum() * f2.getDen()) + (f1.getDen() * f2.getNum());
long den = (f1.getDen() * f2.getDen());
return fraction(num, den);
}
fraction sub(fraction &f1, fraction &f2)
{
long num = (f1.getNum() * f2.getDen()) - (f1.getDen() * f2.getNum());
long den = (f1.getDen() * f2.getDen());
return fraction(num, den);
}
fraction mult(fraction &f1, fraction &f2)
{
long num = (f1.getNum() * f2.getNum());
long den = (f1.getDen() * f2.getDen());
return fraction(num, den);
}
fraction div(fraction &f1, fraction &f2)
{
long num = (f1.getNum() * f2.getDen());
long den = (f1.getDen() * f2.getNum());
return fraction(num, den);
}
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;
}