У меня есть класс Rational
, его фрагмент:
class Rational {
private:
int numerator;
int denominator;
void saveAsIrreducible();
gcd(int x, inty);
public:
Rational(int numerator, int denominator=1);
Rational operator*(Rational &r);
friend ostream& operator<<(ostream &output, Rational &r);
}
И реализация:
Rational::Rational(int numerator, int denominator) {
this->numerator = numerator;
this->denominator = denominator;
this->saveAsIrreducible();
}
Rational Rational::operator*(Rational &r) {
Rational r2 = Rational((this->numerator*r.numerator), (this->denominator*r.denominator));
return r2;
}
ostream& operator<<(ostream &output, Rational &r) {
output << r.numerator;
if(r.denominator!=1)
output << "|" << r.denominator;
return output;
}
void Rational::saveAsIrreducible() {
int greatestCommonDivisor = gcd(numerator, denominator);
numerator = numerator/greatestCommonDivisor;
denominator = denominator/greatestCommonDivisor;
if(numerator > 0 && denominator < 0) { /* for example for 3|-2, after arithmetic operations */
numerator = -numerator;
denominator = -denominator;
}
}
int Rational::gcd(int x, int y) {
while(y!=0) {
int r = x%y;
x = y;
y = r;
}
return x;
}
У меня проблема с использованием оператора *
с cout
. Например, это работает:
Rational r1(3,5), r2(3,6);
Rational r3 = r1*r2;
cout << r3 << endl;
Вывод правильный. Но когда я пытаюсь сделать это:
Rational r1(3,5), r2(3,6);
cout << r1*r2 << endl;
Я получаю много ошибок от компилятора:
Rational.cpp|186|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Rational')|
Rational.cpp|186|error: invalid initialization of non-const reference of type 'Rational&' from an rvalue of type 'Rational'|
Как решить эту проблему?