Could you explain me how to correctly multiply the fraction by the number? Tried to overload, but I get compiler error:
binary operator
*
: the operator accepting the left operand of type of theSimpleFraction
is not found
class SimpleFraction
{
static int findGcd(int a, int b)
{
while (a&&b)
{
if (a > b)
a %= b;
else
b %= a;
}
return (a == 0) ? b : a;
}
private:
int a,
b;
public:
SimpleFraction(int a1 = 0, int a2 = 1) :a(a1), b(a2)
{
if (a2 == 0)
throw std::runtime_error("zero division error");
}
friend ostream& operator<<(ostream&out, const SimpleFraction& f)
{
return out << f.a << "/" << f.b ;
}
friend istream& operator >> (istream&in, SimpleFraction& f)
{
char t;
return in >> f.a >>t>> f.b;
}
friend SimpleFraction operator *(const SimpleFraction&x, int n)
{
int a = x.a *n;
int gcd = finGcd(a, x.b);
return SimpleFraction(a / gcd, x.b / gcd);
}
};
I get that error message , when I enter such code
int main()
{
SimpleFraction SFArray1(2, 2);
SimpleFraction r = SFArray1 * 2;
cout << r;
return 0;
}