I have been working on this code for a while and for some reason I can't figure out why it won't compile I get a lot of errors.
#include "rational.h"
Rational::Rational ()
{
_p = 0;
_q = 1;
}
Rational::Rational (long P, long Q )
{
if ( Q == 0) Q = 1;
_p = P;
_q = Q;
}
Rational::Rational (const Rational& R)
{
_p = R._p;
_q = R._q;
}
//Rational(long, long = 1);//constructor
//Rational(const Rational&);//copy constructor
Rational Rational::operator=(const Rational& R)
{
if (this == &R) return *this;
_p = R._p;
_q = R._q;
return *this;
}
Rational Rational::operator+=(const Rational& R)
{
_p = _p*R._q + R._p*_q;
_q *= R._q;
return *this;
}
Rational Rational::operator-=(const Rational& R)
{
return operator +=(-R);
}
Rational Rational::operator*=(const Rational& R)
{
_p *= R._p;
_q *= R._q;
return *this;
}
Rational Rational::operator/=(const Rational& R)
{
if ( R._p == 0)
{
cout << "devison by 0 error";
cin.get();
}
_p *= R._q;
_q *= R._p;
return *this;
}
Rational Rational::operator+ (const Rational& R) const
{
return Rational (*this)+=R;
}
Rational Rational::operator+ (long L) const
{
return Rational (*this)+=Rational(L);
}
friend Rational Rational::operator+ (long L, const Rational& R)
{
return Rational(L)+=R;
}
Rational Rational::operator- (const Rational& R) const
{
return Rational (*this)-=R;
}
Rational Rational::operator- (long L) const
{
return Rational (*this)-=Rational(L);
}
friend Rational Rational::operator- (long L, const Rational& R)
{
return Rational(L)-=R;
}
Rational Rational::operator* (const Rational& R) const
{
Rational temp(*this);
temp._p *= R._p;
temp._q *= R._q;
return temp;
}
Rational Rational::operator* (long L) const
{
Rational temp(*this);
temp._p *= L;
return temp;
}
friend Rational Rational::operator* (long L, const Rational& R)
{
Rational temp(*R);
temp._p *= L;
return temp;
}
Rational Rational::operator/ (const Rational& R) const
{
Rational temp(*this);
temp._p *= R._q;
temp._q *= R._q;
return temp;
}
Rational Rational::operator/ (long L) const
{
Rational temp(*this);
temp._q *= L;
return temp;
}
friend Rational Rational::operator/ (long L, const Rational& R)
{
Rational temp(*R);
temp._q *= L;
return temp;
}
friend ostream& Rational::operator<< ( ostream& os, const Rational& R)
{
os << R._p << ':' << R._q;
return os;
}
friend istream& Rational::operator>> ( istream& is, Rational& R)
{
long l1,l3;
char c2;
is >> l1 >> c2 >> l3;
R = Rational(l1,l3);
return is;
}
Rational Rational::operator- () const
{
//friend Rational operator- (const Rationa&);
//Rational operator+ (const Rational&) const;
//friend Rational operator+ (const Rationl&, Const Rational&);
return Rational (-_p,_q);
}
bool Rational::operator== (const Rational& R) const
{
return (_p*R._q) == (R._p*_q);
}
bool Rational::operator== (long L) const
{
return *this == Rational(L);
}
friend bool Rational::operator== (long L, const Rational& R)
{
return Rational(L) == R;
}
bool Rational::operator< (const Rational& R) const
{
return (_p*R._q) < (R._p*_q);
}
bool Rational::operator< (long L) const
{
return *this < Rational(L);
}
friend bool Rational::operator< (long L, const Rational& R)
{
return Rational(L) < R;
}
bool Rational::operator> (const Rational& R) const
{
return (*this !=R) && (*this < R);
}
bool Rational::operator> (long L) const
{
return *this > Rational(L);
}
friend bool Rational::operator> (long L, const Rational& R)
{
return Rational(L) > R;
}
bool operator!= (const Rational& R) const;
{
return !((_p*R._q) == (R._p*_q));
}
bool operator!= (long L) const;
{
return *this != Rational(L);
}
friend bool Rational::operator!= (long L, const Rational& R)
{
return Rational(L) != R;
}
bool operator>= (const Rational& R)
{
return (_p*R._q) > (R._p*_q);
}
bool Rational::operator<= (long L) const
{
return *this <= Rational(L);
}
friend bool Rational::operator<= (long L, const Rational& R)
{
return Rational(L) <= R;
}
bool operator>= (const Rational& R) const
{
return (_p*R._q) >= (R._p*_q);
}
bool Rational::operator>= (long L) const
{
return *this >= Rational(L);
}
friend bool Rational::operator>= (long L, const Rational& R)
{
return Rational(L) >= R;
}
Rational operator- () const
{
Rational temp(*this);
//_p = -_p;
temp._p = -temp._p;
return temp;
}
Rational operator+() const
{
return *this;
}
Rational Rational::operator++ (int) //post
{
Rational temp(*this);
_p += _q;
//operator+=(Rational(1));
return temp;
}
Rational Rational::operator-- (int) //post
{
Rational temp(*this);
_p -= _q;
return temp;
}
Rational Rational::operator++ () //pre
{
_p += _q;
return *this;
}
Rational Rational::operator-- () //pre
{
_p -= _q;
return *this;
}
here is rational.h in case it's needed
/**********************************
**********************************/
#ifndef _RATIONAL_H_
#define _RATIONAL_H_ // make sure it is always spelled correct and the same.
#include <iostream>
using namespace std;
class Rational
{
long _p;
long _q;
public:
Rational( ); //default constructor
Rational(long, long = 1);//constructor
Rational(const Rational&);//copy constructor
Rational& operator=(const Rational&);
Rational& operator+=(const Rational&);
Rational& operator-=(const Rational&);
Rational& operator*=(const Rational&);
Rational& operator/=(const Rational&);
friend ostream& operator<< ( ostream&, const Rational&);
friend istream& operator>> ( istream&, Rational&);
Rational operator+ (const Rational& ) const;
Rational operator+ (long) const;
friend Rational operator+ (long, const Rational&);
Rational operator- (const Rational& ) const;
Rational operator- (long) const;
friend Rational operator- (long, const Rational&);
Rational operator- () const;
Rational operator* (const Rational& ) const;
Rational operator* (long) const
friend Rational operator* (long, const Rational&);
Rational operator/ (const Rational& ) const;
Rational operator/ (long) const;
friend Rational operator/ (long, const Rational&);
// Unary operators:
Rational operator- () const;
Rational operator+ () const;
Rational operator++ (int); //post
Rational operator-- (int); //post
Rational& operator++ (); //pre
Rational& operator-- (); //pre
friend ostream& operator;
//void display() const;
bool operator== (const Rational& ) const;
bool operator== (long) const;
friend bool operator== (long, const Rational&);
bool operator< (const Rational& ) const;
bool operator< (long) const;
friend bool operator< (long, const Rational&);
bool operator!= (const Rational& ) const;
bool operator!= (long) const;
friend bool operator!= (long, const Rational&);
bool operator> (const Rational& ) const;
bool operator> (long) const;
friend bool operator> (long, const Rational&);
bool operator<= (const Rational& ) const;
bool operator<= (long) const;
friend bool operator<= (long, const Rational&);
bool operator>= (const Rational& ) const;
bool operator>= (long) const;
friend bool operator>= (long, const Rational&);
}; // dont't forget the ';'
#endif