2

I am working on Exercise 11.11 on page 498 of C++ How to program 8th edition.

The question is this: create a class RationalNumber (fractions) with the following capabilities: a) create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators b) overload the addition, substraction, multiplication and division operators for this class c) overload the relational and equality operators for this class

I'm working on overloading the addition operator.

I have completed a, but I am not getting anywhere on overloading. I have searched the internet for a better explanation, but I am just not grasping the concept of how to overload operators with user defined types.

Each RationalNumber has a numerator and a denominator that combine to make a fraction. I need to add them together, find the common denominator, etc.

My RationalNumber.h is thus:

// RationalNumber.h
// RationalNumber class definition
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

class RationalNumber
{

public:
RationalNumber( int numerator, int denominator);

void setNumerator(int numerator);
int getNumerator();

void setDenominator(int denominator);
int getDenominator();

void printRationalNumber();

RationalNumber operator+(Rationalnumber a, RationalNumber b);

private:
int numerator; //top number
int denominator; //bottom number
};

#endif

Now, just focusing on my operator+ function:

RationalNumber RationalNumber::operator+(RationalNumber a, RationalNumber b)
{

}

Visual Studio is telling me that class Rationaloperator has no member "operator+"

Any help would be appreciated.

Thanks, Eric

UPDATE:

I now have:

RationalNumber operator+(Rationalnumber a);

and

RationalNumber RationalNumber::operator+(RationalNumber a)
{

}

I'm getting error: deceleration is incompatible.....( < error_type > a)

Eric Francis
  • 23,039
  • 31
  • 88
  • 122
  • My C++ skills are not the best anymore, but try to do the hole decleration in the .h file and not in the cpp file. – Felix K. Nov 19 '11 at 19:24
  • Well, I'm trying to stay in tune with the book. So I have a RationalNumber.h, RationalNumber.cpp, Excersise.cpp (contains main) – Eric Francis Nov 19 '11 at 19:25

2 Answers2

9

+ is a binary operator. If you make it a member, it should only take one argument, since the calling object is the left operand.

Also note that you don't have to make it a member. What I usually do is implement operator+= as a member, then operator+ as a free function that utilizes operator+=. This has the advantage that either the left or the right operand can be implicitly converted. For example, if the RationalNumber class had an implicit conversion from double:

RationalNumber r1,r2;
r1 = r2 + 3.14; // this will work either way
r1 = 3.14 + r2; // can't do this if operator+ is a member

As for your error about incompatible declarations:

RationalNumber operator+(Rationalnumber a);
                                 ^
                       Should be a capital N
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • I now have: RationalNumber operator+(Rationalnumber a); and RationalNumber RationalNumber::operator+(RationalNumber a) { } I'm getting error: deceleration type is incompatible....( a) Thanks, Eric – Eric Francis Nov 19 '11 at 19:31
  • Additional: http://en.wikibooks.org/wiki/C++_Programming/Operators/Operator_Overloading There are also some samples where the `+` takes two parameters. – Felix K. Nov 19 '11 at 19:31
  • `+` can be defined as a unary operator also, just like `-`. In this case, a member function would take zero arguments and a nonmember function would take one argument. – JohnPS Nov 19 '11 at 22:19
1

you may want to consider this faq article on operator overloading in c++. It gives the following example on overloading the arithmetic operators.

class X { 
  X& operator+=(const X& rhs) 
  {
    // actual addition of rhs to *this 
    return *this;
  }
}; 
inline X operator+(X lhs, const X& rhs)
{
  lhs += rhs;
  return lhs;
} 

Note that the operator+ is implemented outside the class, using the operator+= member function. Also note that the second argument for the operator+ is passed by const reference, which avoids copying the object, thus improving the efficiency of your code.

I don't know whether that fixes your problem, but it's consideration is certainly worth your time.

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187