Leeker
Leeker

Reputation: 29

I got error C2296 and C3867 when I tried to overload operator

I want to overload operators( as free functions) for working with fractions but got some errors(23). My intuition is telling me that problem with const or smth like that. It's a little bit complicated for me now. So how to fix this or get this theme better?

#include <iostream>
#include <cmath>

using namespace std;

int GetGsd(int a, int b) {      // Greatest common divisor
    while (a > 0 && b > 0) {
        if (a > b) {
            a = a % b;
        }
        else {
            b = b % a;
        }
    }
    return a + b;
}

char GetSignOfFraction(int a, int b) {
    if (a >= 0 && b > 0 || a <= 0 && b < 0) {
        return '+';
    }
    else if (a < 0 || b < 0) {
        return '-';
    }
}

class Rational {    // default c
public:
    Rational() {
        num = 0;
        den = 1;
    }

    Rational(const int numerator, const int denominator) {    
        num = numerator / GetGsd(numerator, denominator);
        den = denominator / GetGsd(numerator, denominator);
    }
    Rational& operator=(const Rational& r) {  // overloaded = 
        Rational a{ r.num,r.den };
        return a;
    }


    int Numerator() const {
        char sign = GetSignOfFraction(num, den);
        if (sign == '+') {
            return  fabs(num / GetGsd(num, den));
        }
        else {
            return num / GetGsd(num, den);
        }

    }

    int Denominator() const {
        if (num == 0) {
            return 1;
        }
        return fabs(den / GetGsd(num, den));
    }

private:
    int num;
    int den;
};

Rational operator+(const Rational& lhs, const Rational& rhs) { // overloaded +   // problem is there
    Rational a;
    a.Denominator = rhs.Denominator * lhs.Denominator;
    a.Numerator = rhs.Numerator * lhs.Denominator + lhs.Numerator * rhs.Denominator;

    a.Numerator /= GetGsd(a.Numerator, a.Denominator);
    a.Denominator /= GetGsd(a.Numerator, a.Denominator);
    return a;

}

bool operator==(const Rational& lhs, const Rational& rhs) {  // and there
    if (lhs.Numerator == rhs.Numerator && lhs.Denominator == rhs.Denominator) {
        return true;
    }
    return false;
}



int main()
{

}

Upvotes: 0

Views: 54

Answers (1)

Object object
Object object

Reputation: 2054

There are several problems with your code here.

You are getting the errors from this line and the lines immediatly after it

a.Denominator = rhs.Denominator * lhs.Denominator;

There are two main problems here.

  1. You are trying to assign the value of a member method instead of calling it
  2. Even if you called it you are trying to assign a value to a returned constint

You can solve the first issue by simply adding brackets to the method call a.Denominator(), however the second error is more complex. If you want to change the value of a.den (which I think is what your trying to do) you will need to write a second acessor which can be used for setting the value.

void Denominator(int denominator){
        den = denominator
}

Then call it with a.Denominator(/* My new value of a.den */)

Upvotes: 1

Related Questions