For some reason I can't figure out the + operator. I can do += just fine, but when I try to do +, I keep getting the address of the object instead of the value. The program is dealing with fractions and simplifying them with Euclid's algorithm (to explain the class name). The code:
Basic class definition:
Code:
class Euclid
{
public:
//bunch of functions
...
private:
int numerator;
int denominator;
int simplify(int, int);
};
This is +=, which works perfectly:
Code:
Euclid &Euclid::operator +=(Euclid &fract)
{
numerator = ((numerator * fract.denominator) + (denominator * fract.numerator));
denominator = (denominator * fract.denominator);
int gcd = simplify(numerator, denominator);
numerator = (numerator)/gcd;
denominator = (denominator)/gcd;
return *this;
}
This is +, which sucks:
Code:
Euclid &Euclid::operator +(Euclid &fract)
{
Euclid temp(1,1);
temp.numerator = ((numerator * fract.denominator) + (denominator * fract.numerator));
temp.denominator = (denominator * fract.denominator);
int gcd = simplify(temp.numerator, temp.denominator);
temp.numerator = (temp.numerator)/gcd;
temp.denominator = (temp.denominator)/gcd;
return temp;
}
And this is how they are being used in the actual program:
Code:
cout<<"Adding 1/2 to this fraction..."<<endl;
test = userfrac + half;
cout<<"The reduced fraction is "<< test.getnum() <<"/"<< test.getdem() <<"."<<endl;