1

I am fairly new in C++ (I have spent all my life in C so I thought it's time to invest some time in learning a new language in order to enrich my knowledge :) ). I have a class named "Rational", I have all its specific functions for getters, setters, constructors, etc.(it's not relevant here). The interesting part is when I try to overload the +,-,,/ operators. I am able to successfully do this between two Rational objects, for example Rational a(1,5),b(5,5),c; c = a + b; so all this works just fine. Now I am trying to upgrade my class by trying to +,-,,/ between a Rational and an integer, for example 2 + a, 10 - b etc. Here is (a snippet of)my code for overloading between Rationals:

Rational.cc

...
    Rational Rational::operator+(Rational B) {
            int Num;
            int Den;

            Num = p * B.q + q * B.p;
            Den = q * B.q;

            Rational C(Num, Den);
            C.simplifierFraction();
            return C;
    }

    Rational Rational::operator-(Rational B) {
            int Num;
            int Den;        

            Num = p * B.q - q * B.p;
            Den = q * B.q;

            Rational C(Num, Den);
            C.simplifierFraction();
            return C;
    }



    Rational Rational::operator*(Rational B) 
    {
            int Num;
            int Den;

            Num = p * B.p;
            Den = q * B.q;

            Rational C(Num, Den);
            C.simplifierFraction();
            return C;
    }

    Rational Rational::operator/(Rational B) 
    {
            int Num;
            int Den;
            Rational invB = inverse(B);

            Num = p * invB.p;
            Den = q * invB.q;

            Rational C(Num, Den);
            C.simplifierFraction();
            return C;
    }
...

Rational.h

    Rational operator+(Rational B);
    Rational operator-(Rational B);
    Rational operator*(Rational B);
    Rational operator/(Rational B);


private:
    int p;
    int q;
protected:

TestRat.cc

int main() {
...
    const Rational demi(1,2);      
    const Rational tiers(1,3);      
    const Rational quart(1,4);
    r0 = demi + tiers - quart;         
    r1 = 1 + demi;                     
    r2 = 2 - tiers;                    
    r3 = 3 * quart;                    
    r4 = 1 / r0;
...  

So when I try to run TestRat.cc it says:

testrat.cc:31: error: no match for ‘operator+’ in ‘1 + r9’
testrat.cc:52: error: passing ‘const Rational’ as ‘this’ argument of ‘Rational Rational::operator+(Rational)’ discards qualifiers
testrat.cc:53: error: no match for ‘operator+’ in ‘1 + demi’
testrat.cc:54: error: no match for ‘operator-’ in ‘2 - tiers’
testrat.cc:55: error: no match for ‘operator*’ in ‘3 * quart’
testrat.cc:56: error: no match for ‘operator/’ in ‘1 / r0’

What must I do in order to be able to make this work? Thanks!

André Hincu
  • 427
  • 3
  • 5
  • 13
  • 1
    Make them free functions instead. – chris Nov 28 '12 at 23:14
  • 1
    Or make the operators `const` (which they should be anyway). Your immediate operators need free functions (and either make them friends or exploit your existing ops through a temp). – WhozCraig Nov 28 '12 at 23:15
  • make a non-member function that looks like this: Rational operator+(int i, Rational B){...} – cppguy Nov 28 '12 at 23:17

1 Answers1

3

tl;dr:

your operators should be declared as:

Rational operator+(const Rational& B) const;

well... these ones, at least. operator = should return a reference to *this, but that's outside the scope of this questions. Also, those operators are defined to work on Rational objects, whereas

r1 = 1 + demi; 

attempts to operate on an int and a Rational object. You'll have to define outside the class an appropriate operator:

inline Rational operator+(int, const Rational& r)
{
    //...
}

I suggest you start learning C++ with a good book though. Just picking up stuff from here and there isn't really going to work.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • OK, I see what you mean. So I have to change my operators? Or I have to create new operators outside the scope of the class Rational(as you described earlier?) – André Hincu Nov 28 '12 at 23:36
  • What I mean is, can I make a function to manage both int+Rational and Rational+Rational, or I have to do them separately(I mean just add another function outside the scope)? – André Hincu Nov 28 '12 at 23:37
  • 1
    @AndréHincu add a new function outside the class. – Luchian Grigore Nov 28 '12 at 23:40
  • Ok, so my method operators are just fine (well, outside the fact that I had forgotten to put "const" at the end, I fixed it now). I just had to add new functions outside the class, different from the method. Did I get it right? – André Hincu Nov 28 '12 at 23:43
  • @AndréHincu in a nutshell, yes. – Luchian Grigore Nov 28 '12 at 23:53