I have an error that I couldn't solve in my code.
If I call my function in main as the following :
Ratio f34(3L,4L);
const Ratio one(f34.divide(f34));
or
Ratio s = Ratio(7).divide(Ratio(8));
I have an error that says
error: cannot
bind non-const lvalue reference of type ‘Ratio&’ to an
rvalue of type ‘Ratio’
and here is my Ratio constructor and divide function :
Ratio::Ratio( long numerator, long denominator) : nu(numerator) , de(denominator) {
if (denominator == 0) {
throw std::runtime_error("Cannot divide by 0");
}
}
Ratio Ratio::divide(Ratio r) {
long num = r.de *nu;
long den = de * r.nu;
Ratio result(num,den);
return result;
}
Note : nu and de are private variables in Ratio class.
I tried to change divide argument to be ( const Ratio &r ), but that did not solve the problem. I still have the same error.
What I understand is that the compiler complained that he can not make a temporary copy of an object. I still have no idea how can I solve it. I tried every thing!! please help :).
Aucun commentaire:
Enregistrer un commentaire