Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ overloading the (+) operatior
    text
    copied!<p>I am working on Exercise 11.11 on page 498 of C++ How to program 8th edition.</p> <p>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</p> <p>I'm working on overloading the addition operator.</p> <p>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.</p> <p>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.</p> <p>My RationalNumber.h is thus:</p> <pre><code>// RationalNumber.h // RationalNumber class definition #ifndef RATIONALNUMBER_H #define RATIONALNUMBER_H #include &lt;iomanip&gt; #include &lt;iostream&gt; #include &lt;string&gt; 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 </code></pre> <p>Now, just focusing on my operator+ function:</p> <pre><code>RationalNumber RationalNumber::operator+(RationalNumber a, RationalNumber b) { } </code></pre> <p>Visual Studio is telling me that class Rationaloperator has no member "operator+"</p> <p>Any help would be appreciated.</p> <p>Thanks, Eric</p> <p><strong>UPDATE:</strong></p> <p>I now have:</p> <pre><code>RationalNumber operator+(Rationalnumber a); </code></pre> <p>and</p> <pre><code>RationalNumber RationalNumber::operator+(RationalNumber a) { } </code></pre> <p>I'm getting error: deceleration is incompatible.....( &lt; error_type > a)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload