title: no match for operator+ error in c++
body: <p>Here is the Rational class that i've been working on:</p>
  
  <p>rational.h</p>
  
  <pre><code>#include&lt;iostream&gt;
  
  using namespace std;
  
  #ifndef RATIONAL_H
  #define RATIONAL_H
  
  class Rational
  {
    int numerator,denominator;
    public:
    // the various constructors
    Rational();
    Rational(int);
    Rational(int,int);
  
    //member functions
    int get_numerator()const{return numerator;}
    int get_denominator()const{return denominator;}
  
    // overloaded operators
    // relational operators
    bool operator==(const Rational&amp;)const;
    bool operator&lt;(const Rational&amp;)const;
    bool operator&lt;=(const Rational&amp;)const;
    bool operator&gt;(const Rational&amp;)const;
    bool operator&gt;=(const Rational&amp;)const;
  
    //arithmetic operators
    Rational operator+(const Rational&amp;);
    Rational operator-(const Rational&amp;);
    Rational operator*(const Rational&amp;);
    Rational operator/(const Rational&amp;);
  
    //output operator
    friend ostream&amp; operator&lt;&lt;(ostream&amp;, const Rational&amp;);
  };
  #endif //RATIONAL_H
  </code></pre>
  
  <p>rational.cpp</p>
  
  <pre><code>#include "rational.h"
  
  // implementation ofthe various constructors
  Rational::Rational()
    :numerator(0),denominator(1){}
  
  Rational::Rational(int number)
    :numerator(number),denominator(1){}
  
  Rational::Rational(int n,int d)
    :numerator(n),denominator(d)
  {
    if(denominator == 0) denominator = 1;
    if(denominator &lt; 0)
    {
      numerator *= -1;
      denominator *= -1;
    }
  }
  
  // implementation of overloaded operators
  bool Rational::operator==(const Rational&amp; rhs)const
  {
    if( numerator * rhs.get_denominator() == denominator * rhs.get_numerator() )
    {
      return true;
    }
    else return false;
  }
  
  bool Rational::operator&lt;(const Rational&amp; rhs)const
  {
    if( numerator * rhs.get_denominator() &lt; denominator * rhs.get_numerator() )
    {
      return true;
    }
    else return false;
  }
  
  bool Rational::operator&lt;=(const Rational&amp; rhs)const
  {
    return operator==(rhs) || operator&lt;(rhs);
  }
  
  bool Rational::operator&gt;(const Rational&amp; rhs)const
  {
    return !operator&lt;(rhs);
  }
  
  bool Rational::operator&gt;=(const Rational&amp; rhs)const
  {
    return operator==(rhs) || operator&gt;(rhs);
  }
  
  //arithmetic operators
  Rational Rational::operator+(const Rational&amp; rhs)
  {
    return Rational( (numerator * rhs.get_denominator() + denominator*rhs.get_numerator()), (denominator * rhs.get_denominator()) );
  }
  
  Rational Rational::operator-(const Rational&amp; rhs)
  {
    //reuse of the + operator for substraction
    return operator+(Rational(-1*rhs.get_numerator(),rhs.get_denominator()));
  }
  
  Rational Rational::operator*(const Rational&amp; rhs)
  {
    return Rational(numerator * rhs.get_numerator(), denominator * rhs.get_denominator());
  }
  
  Rational Rational::operator/(const Rational&amp; rhs)
  {
    //reuse of the * operator as division is the inverse of multiplication
    return operator*(Rational(rhs.get_denominator(),rhs.get_numerator()));
  }
  
  // friend output operator
  ostream&amp; operator&lt;&lt;(ostream&amp; os, const Rational&amp; r)
  {
     os&lt;&lt;r.get_numerator()&lt;&lt;"/"&lt;&lt;r.get_denominator();
     return os;
  }
  
  and the driver for the program driver.cpp
  #include "rational.h"
  
  int main()
  {
    Rational r1(),r2(3),r3(11,3),tmp;
    cout&lt;&lt;r1+r2&lt;&lt;endl;
    cout&lt;&lt;r2&lt;&lt;endl;
    cout&lt;&lt;r2-r3&lt;&lt;endl;
    cout&lt;&lt;r2*r3&lt;&lt;endl;
    cout&lt;&lt;r1/r3;
  
    return 0;
  }
  </code></pre>
  
  <p>Here's the error that i get when trying to compile it.</p>
  
  <pre><code>driver.cpp: In function ‘int main()’:
  driver.cpp:6:12: error: no match for ‘operator+’ in ‘r1 + r2’
  driver.cpp:6:12: note: candidates are:
  /usr/include/c++/4.6/bits/stl_iterator.h:327:5: note: template&lt;class _Iterator&gt; std::reverse_iterator&lt;_Iterator&gt; std::operator+(typename std::reverse_iterator&lt;_Iterator&gt;::difference_type, const std::reverse_iterator&lt;_Iterator&gt;&amp;)
  /usr/include/c++/4.6/bits/basic_string.h:2306:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; std::basic_string&lt;_CharT, _Traits, _Alloc&gt; std::operator+(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;, const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;)
  /usr/include/c++/4.6/bits/basic_string.tcc:694:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; std::basic_string&lt;_CharT, _Traits, _Alloc&gt; std::operator+(const _CharT*, const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;)
  /usr/include/c++/4.6/bits/basic_string.tcc:710:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; std::basic_string&lt;_CharT, _Traits, _Alloc&gt; std::operator+(_CharT, const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;)
  /usr/include/c++/4.6/bits/basic_string.h:2343:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; std::basic_string&lt;_CharT, _Traits, _Alloc&gt; std::operator+(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;, const _CharT*)
  /usr/include/c++/4.6/bits/basic_string.h:2359:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; std::basic_string&lt;_CharT, _Traits, _Alloc&gt; std::operator+(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;, _CharT)
  driver.cpp:10:12: error: no match for ‘operator/’ in ‘r1 / r3’
  </code></pre>
  
  <p>When I comment the lines where I use the operator+ and operator/, then the code works.
  This baffles me as I have implemented the operator- using operator+ and similarly the operator/ using operator*.
  So if one of them works, I'd figure that the other would work too.
  Can someone please explain what I have done wrong here?</p>
  
  <p>Edit
  I get even more errors when I use the operator==</p>
  
  <pre><code>rational.cpp:22:6: error: prototype for ‘bool Rational::operator==(const Rational&amp;)’ does not match any in class ‘Rational’
  rational.h:23:8: error: candidate is: bool Rational::operator==(Rational)
  rational.cpp:31:6: error: prototype for ‘bool Rational::operator&lt;(const Rational&amp;) const’ does not match any in class ‘Rational’
  rational.h:24:8: error: candidate is: bool Rational::operator&lt;(Rational)
  driver.cpp: In function ‘int main()’:
  driver.cpp:11:10: error: no match for ‘operator==’ in ‘r1 == tmp’
  driver.cpp:11:10: note: candidates are:
  /usr/include/c++/4.6/bits/postypes.h:218:5: note: template&lt;class _StateT&gt; bool std::operator==(const std::fpos&lt;_StateT&gt;&amp;, const std::fpos&lt;_StateT&gt;&amp;)
  /usr/include/c++/4.6/bits/stl_pair.h:201:5: note: template&lt;class _T1, class _T2&gt; bool std::operator==(const std::pair&lt;_T1, _T2&gt;&amp;, const std::pair&lt;_T1, _T2&gt;&amp;)
  /usr/include/c++/4.6/bits/stl_iterator.h:285:5: note: template&lt;class _Iterator&gt; bool std::operator==(const std::reverse_iterator&lt;_Iterator&gt;&amp;, const std::reverse_iterator&lt;_Iterator&gt;&amp;)
  /usr/include/c++/4.6/bits/stl_iterator.h:335:5: note: template&lt;class _IteratorL, class _IteratorR&gt; bool std::operator==(const std::reverse_iterator&lt;_IteratorL&gt;&amp;, const std::reverse_iterator&lt;_IteratorR&gt;&amp;)
  /usr/include/c++/4.6/bits/allocator.h:122:5: note: template&lt;class _T1, class _T2&gt; bool std::operator==(const std::allocator&lt;_T1&gt;&amp;, const std::allocator&lt;_T2&gt;&amp;)
  /usr/include/c++/4.6/bits/allocator.h:127:5: note: template&lt;class _Tp&gt; bool std::operator==(const std::allocator&lt;_Tp1&gt;&amp;, const std::allocator&lt;_Tp1&gt;&amp;)
  /usr/include/c++/4.6/bits/basic_string.h:2427:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; bool std::operator==(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;, const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;)
  /usr/include/c++/4.6/bits/basic_string.h:2434:5: note: template&lt;class _CharT&gt; typename __gnu_cxx::__enable_if&lt;std::__is_char&lt;_Tp&gt;::__value, bool&gt;::__type std::operator==(const std::basic_string&lt;_CharT&gt;&amp;, const std::basic_string&lt;_CharT&gt;&amp;)
  /usr/include/c++/4.6/bits/basic_string.h:2448:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; bool std::operator==(const _CharT*, const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;)
  /usr/include/c++/4.6/bits/basic_string.h:2460:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; bool std::operator==(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;, const _CharT*)
  /usr/include/c++/4.6/bits/streambuf_iterator.h:194:5: note: template&lt;class _CharT, class _Traits&gt; bool std::operator==(const std::istreambuf_iterator&lt;_CharT, _Traits&gt;&amp;, const std::istreambuf_iterator&lt;_CharT, _Traits&gt;&amp;)
  </code></pre>
  
  <p>What do these cryptic messages mean?</p>