Here is the Rational class I was working on:
rational.h
#include<iostream>
using namespace std;
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
int numerator,denominator;
public:
Rational();
Rational(int);
Rational(int,int);
int get_numerator()const{return numerator;}
int get_denominator()const{return denominator;}
bool operator==(const Rational&)const;
bool operator<(const Rational&)const;
bool operator<=(const Rational&)const;
bool operator>(const Rational&)const;
bool operator>=(const Rational&)const;
Rational operator+(const Rational&);
Rational operator-(const Rational&);
Rational operator*(const Rational&);
Rational operator/(const Rational&);
friend ostream& operator<<(ostream&, const Rational&);
};
#endif
rational.cpp
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 < 0)
{
numerator *= -1;
denominator *= -1;
}
}
bool Rational::operator==(const Rational& rhs)const
{
if( numerator * rhs.get_denominator() == denominator * rhs.get_numerator() )
{
return true;
}
else return false;
}
bool Rational::operator<(const Rational& rhs)const
{
if( numerator * rhs.get_denominator() < denominator * rhs.get_numerator() )
{
return true;
}
else return false;
}
bool Rational::operator<=(const Rational& rhs)const
{
return operator==(rhs) || operator<(rhs);
}
bool Rational::operator>(const Rational& rhs)const
{
return !operator<(rhs);
}
bool Rational::operator>=(const Rational& rhs)const
{
return operator==(rhs) || operator>(rhs);
}
Rational Rational::operator+(const Rational& rhs)
{
return Rational( (numerator * rhs.get_denominator() + denominator*rhs.get_numerator()), (denominator * rhs.get_denominator()) );
}
Rational Rational::operator-(const Rational& rhs)
{
return operator+(Rational(-1*rhs.get_numerator(),rhs.get_denominator()));
}
Rational Rational::operator*(const Rational& rhs)
{
return Rational(numerator * rhs.get_numerator(), denominator * rhs.get_denominator());
}
Rational Rational::operator/(const Rational& rhs)
{
return operator*(Rational(rhs.get_denominator(),rhs.get_numerator()));
}
ostream& operator<<(ostream& os, const Rational& r)
{
os<<r.get_numerator()<<"/"<<r.get_denominator();
return os;
}
and the driver for the program driver.cpp
int main()
{
Rational r1(),r2(3),r3(11,3),tmp;
cout<<r1+r2<<endl;
cout<<r2<<endl;
cout<<r2-r3<<endl;
cout<<r2*r3<<endl;
cout<<r1/r3;
return 0;
}
Here is the error I get when trying to compile it.
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<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/basic_string.h:2306:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.tcc:694:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.tcc:710:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2343:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2359:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
driver.cpp:10:12: error: no match for โoperator/โ in โr1 / r3โ
When I comment out lines where I use the + operator and the / operator, then the code works. This puzzles me, because I implemented the operator using the + operator and similarly the / operator using the * operator. Therefore, if one of them works, I would understand that the other will work. Can someone explain what I did wrong here?
Edit I get even more errors when using the == operator
rational.cpp:22:6: error: prototype for โbool Rational::operator==(const Rational&)โ 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<(const Rational&) constโ does not match any in class โRationalโ
rational.h:24:8: error: candidate is: bool Rational::operator<(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<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/include/c++/4.6/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/include/c++/4.6/bits/basic_string.h:2427:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
/usr/include/c++/4.6/bits/basic_string.h:2448:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2460:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/streambuf_iterator.h:194:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
What do these critical messages mean?