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

Recommended Answers

All 3 Replies

whats the error? post exactly

In file included from rational.cpp:1:0:
rational.h:40:28: error: expected ‘;’ at end of member declaration
rational.h:48:11: error: ‘Rational Rational::operator-() const’ cannot be overloaded
rational.h:38:11: error: with ‘Rational Rational::operator-() const’
rational.h:54:26: error: expected type-specifier before ‘;’ token
rational.cpp:24:10: error: prototype for ‘Rational Rational::operator=(const Rational&)’ does not match any in class ‘Rational’
rational.h:25:12: error: candidate is: Rational& Rational::operator=(const Rational&)
rational.cpp:32:10: error: prototype for ‘Rational Rational::operator+=(const Rational&)’ does not match any in class ‘Rational’
rational.h:26:12: error: candidate is: Rational& Rational::operator+=(const Rational&)
rational.cpp:39:10: error: prototype for ‘Rational Rational::operator-=(const Rational&)’ does not match any in class ‘Rational’
rational.h:27:12: error: candidate is: Rational& Rational::operator-=(const Rational&)
rational.cpp:43:10: error: prototype for ‘Rational Rational::operator*=(const Rational&)’ does not match any in class ‘Rational’
rational.h:28:12: error: candidate is: Rational& Rational::operator*=(const Rational&)
rational.cpp:50:10: error: prototype for ‘Rational Rational::operator/=(const Rational&)’ does not match any in class ‘Rational’
rational.h:29:12: error: candidate is: Rational& Rational::operator/=(const Rational&)
rational.cpp:72:1: error: ‘friend’ used outside of class
rational.cpp:72:63: error: ‘Rational Rational::operator+(long int, const Rational&)’ must take either zero or one argument
rational.cpp:87:1: error: ‘friend’ used outside of class
rational.cpp:87:63: error: ‘Rational Rational::operator-(long int, const Rational&)’ must take either zero or one argument
rational.cpp:105:1: error: ‘friend’ used outside of class
rational.cpp:105:63: error: ‘Rational Rational::operator*(long int, const Rational&)’ must take either zero or one argument
rational.cpp:127:1: error: ‘friend’ used outside of class
rational.cpp:127:63: error: ‘Rational Rational::operator/(long int, const Rational&)’ must take exactly one argument
rational.cpp:135:1: error: ‘friend’ used outside of class
rational.cpp:135:70: error: ‘std::ostream& Rational::operator<<(std::ostream&, const Rational&)’ must take exactly one argument
rational.cpp:141:1: error: ‘friend’ used outside of class
rational.cpp:141:64: error: ‘std::istream& Rational::operator>>(std::istream&, Rational&)’ must take exactly one argument
rational.cpp:168:1: error: ‘friend’ used outside of class
rational.cpp:168:60: error: ‘bool Rational::operator==(long int, const Rational&)’ must take exactly one argument
rational.cpp:183:1: error: ‘friend’ used outside of class
rational.cpp:183:59: error: ‘bool Rational::operator<(long int, const Rational&)’ must take exactly one argument
rational.cpp:198:1: error: ‘friend’ used outside of class
rational.cpp:198:59: error: ‘bool Rational::operator>(long int, const Rational&)’ must take exactly one argument
rational.cpp:202:37: error: non-member function ‘bool operator!=(const Rational&)’ cannot have cv-qualifier
rational.cpp:202:37: error: ‘bool operator!=(const Rational&)’ must take exactly two arguments
rational.cpp:203:1: error: expected unqualified-id before ‘{’ token
rational.cpp:207:26: error: non-member function ‘bool operator!=(long int)’ cannot have cv-qualifier
rational.cpp:207:26: error: ‘bool operator!=(long int)’ must have an argument of class or enumerated type
rational.cpp:208:1: error: expected unqualified-id before ‘{’ token
rational.cpp:212:1: error: ‘friend’ used outside of class
rational.cpp:212:60: error: ‘bool Rational::operator!=(long int, const Rational&)’ must take exactly one argument
rational.cpp:217:35: error: ‘bool operator>=(const Rational&)’ must take exactly two arguments
rational.cpp:227:1: error: ‘friend’ used outside of class
rational.cpp:227:60: error: ‘bool Rational::operator<=(long int, const Rational&)’ must take exactly one argument
rational.cpp:232:37: error: non-member function ‘bool operator>=(const Rational&)’ cannot have cv-qualifier
rational.cpp:232:37: error: ‘bool operator>=(const Rational&)’ must take exactly two arguments
rational.cpp:242:1: error: ‘friend’ used outside of class
rational.cpp:242:60: error: ‘bool Rational::operator>=(long int, const Rational&)’ must take exactly one argument
rational.cpp:247:23: error: non-member function ‘Rational operator-()’ cannot have cv-qualifier
rational.cpp:247:23: error: ‘Rational operator-()’ must have an argument of class or enumerated type
rational.cpp:255:22: error: non-member function ‘Rational operator+()’ cannot have cv-qualifier
rational.cpp:255:22: error: ‘Rational operator+()’ must have an argument of class or enumerated type
rational.cpp:274:10: error: prototype for ‘Rational Rational::operator++()’ does not match any in class ‘Rational’
rational.h:52:12: error: candidates are: Rational& Rational::operator++()
rational.cpp:260:10: error:                 Rational Rational::operator++(int)
rational.cpp:280:10: error: prototype for ‘Rational Rational::operator--()’ does not match any in class ‘Rational’
rational.h:53:12: error: candidates are: Rational& Rational::operator--()
rational.cpp:268:10: error:                 Rational Rational::operator--(int)

I did catch some missing ';'s and i commented out friend ostream& operation because there was a double.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.