Reputation: 1
This code is a Fraction that adds/subtracts multiple inputs of fractions. Here is my header file:
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class Fraction{
public:
Fraction(int , int );
int fraction(int,int);
void reduce_fraction(int *, int *);
Fraction& operator+(const Fraction&);
Fraction& operator-(const Fraction&);
friend ostream& operator<<(ostream &os, const Fraction& n);
friend istream& operator>>(istream &is, const Fraction& n);
};
#endif
and here is the overloading the operator code which results in an error of invalid initialization of non-const reference of type'Fractions&' from an rvalue of type 'Frations' for all three overloads
Fraction& Fraction::operator+(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) + (n.denom * n.nump);
return Fraction(numera,denom);
}
Fraction& Fraction::operator-(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) - (n.denom* n.nump);
return Fraction(numera, denom);
}
Fraction& Fraction::operator=(const Fraction& n){
if(this==&n) return *this;
return n;
}
Upvotes: 0
Views: 68
Reputation: 118352
Your assignment operator returns a Fraction &
return n;
This is a const Fraction &
This is the error the compiler is complaining about. You're trying to return a reference to a const object, when the function returns a reference to a non-const object.
Upvotes: 0