Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Solved

Posted on 2005-05-01

>>This code does not compile, please advise. Del

*************************code**************************

#ifndef RAT_H

#define RAT_H

class Rational {

public:

Rational(int t = 0, int n = 1);

friend Rational operator + (const Rational& lVal, const Rational& rVal);

friend Rational operator - (const Rational& lVal, const Rational& rVal);

friend Rational operator * (const Rational& lVal, const Rational& rVal);

friend Rational operator / (const Rational& lVal, const Rational& rVal);

Rational& operator += (const Rational& rVal);

Rational& operator -= (const Rational& rVal);

Rational& operator *= (const Rational& rVal);

Rational& operator /= (const Rational& rVal);

friend int operator < (const Rational& lVal, const Rational& rVal);

friend int operator <= (const Rational& lVal, const Rational& rVal);

friend int operator == (const Rational& lVal, const Rational& rVal);

friend int operator != (const Rational& lVal, const Rational& rVal);

friend int operator >= (const Rational& lVal, const Rational& rVal);

friend int operator > (const Rational& lVal, const Rational& rVal);

friend ostream& operator << (ostream& os, const Rational& rat);

friend istream& operator >> (istream& is, Rational& rat);

private:

int teller, number;

void reduce();

int number(int, int);

};

Rational::Rational(int t, int n): teller(t), number(n) {

reduce();

}

Rational Rational::operator -() const {

return Rational(-teller, number);

}

// operator overloading

Rational operator + (const Rational& lVal, const Rational& rVal) {

Rational result;

result.teller = lVal.teller * rVal.number + lVal.number * rVal.teller;

result.number = lVal.number * rVal.number;

result.reduce();

return result;

}

Rational operator - (const Rational& lVal, const Rational& rVal) {

Rational result;

result = lVal +- rVal;

result.reduce();

return result;

}

Rational operator * (const Rational& lVal, const Rational& rVal) {

Rational result;

result.teller = lVal.teller * rVal.teller;

result.number = lVal.number * rVal.number;

result.reduce();

return result;

}

Rational operator / (const Rational& lVal, const Rational& rVal) {

Rational result;

result.teller = lVal.teller * rVal.number;

result.number = lVal.number * rVal.teller;

result.reduce();

return result;

}

Rational& operator += (const Rational& rVal) {

teller = teller * rVal.number + number * rVal.teller;

number = number * rVal.number;

reduce();

return *this;

}

Rational& operator -= (const Rational& rVal) {

this->operator += (-rVal);

reduce();

return *this;

}

Rational& operator *= (const Rational& rVal) {

teller = teller * rVal.teller;

number = number * rVal.number;

reduce();

return *this;

}

Rational& operator /= (const Rational& rVal) {

teller = teller * rVal.number;

number = number * rVal.teller;

reduce();

return *this;

}

int operator < (const Rational& lVal, const Rational& rVal) {

return(lVal.toDouble() < rVal.toDouble());

}

int operator <= (const Rational& lVal, const Rational& rVal) {

return(lVal < rVal || lVal == rVal);

}

int operator == (const Rational& lVal, const Rational& rVal) {

return lVal.teller * rVal.number == lVal.number * rVal.teller;

}

int operator != (const Rational& lVal, const Rational& rVal) {

return !(lVal == rVal);

}

int operator >= (const Rational& lVal, const Rational& rVal) {

return(lVal > rVal || lVal == rVal);

}

int operator > (const Rational& lVal, const Rational& rVal) {

return(lVal.toDouble() > rVal.toDouble());

}

istream& operator >> (istream& is, Rational& rat) {

int t, n;

char deel;

cin >> is >> t >> deel >> n;

rat = Rational(t,n);

return is;

}

#endif

********************

// main.cpp

#include "rat.h"

#include <iostream>

#include <cstring>

#include <cstdlib>

using namespace std;

int main() {

Rational r;

r.clrscr();

r.Rational b1;

r.Rational b2;

cout << "Input the number in the form (a/b): ";

cin >> b1 >> b2;

// Test for +, -, *, /

cout << b1 << " + " << b2 << " = " << b1 + b2 << endl;

cout << b1 << " - " << b2 << " = " << b1 - b2 << endl;

cout << b1 << " * " << b2 << " = " << b1 * b2 << endl;

cout << b1 << " / " << b2 << " = " << b1 / b2 << endl;

// test program for overloaded operators

cout << b1 << " < " << b2;

(b1 < b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " <= " << b2;

(b1 <= b2) ? cout << " [X]" << endl << endl : cout << " [ ]" << endl << endl;

cout << b1 << " == " << b2;

(b1 == b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " != " << b2;

(b1 != b2) ? cout << " [X]" << endl << endl : cout << " [ ]" << endl << endl;

cout << b1 << " >= " << b2;

(b1 >= b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " > " << b2;

(b1 > b2) ? cout << " [X]" << endl : cout << " [ ]" << endl << endl;

getch();

return 0;

}

*************

output = multiple errors.

*************************c

#ifndef RAT_H

#define RAT_H

class Rational {

public:

Rational(int t = 0, int n = 1);

friend Rational operator + (const Rational& lVal, const Rational& rVal);

friend Rational operator - (const Rational& lVal, const Rational& rVal);

friend Rational operator * (const Rational& lVal, const Rational& rVal);

friend Rational operator / (const Rational& lVal, const Rational& rVal);

Rational& operator += (const Rational& rVal);

Rational& operator -= (const Rational& rVal);

Rational& operator *= (const Rational& rVal);

Rational& operator /= (const Rational& rVal);

friend int operator < (const Rational& lVal, const Rational& rVal);

friend int operator <= (const Rational& lVal, const Rational& rVal);

friend int operator == (const Rational& lVal, const Rational& rVal);

friend int operator != (const Rational& lVal, const Rational& rVal);

friend int operator >= (const Rational& lVal, const Rational& rVal);

friend int operator > (const Rational& lVal, const Rational& rVal);

friend ostream& operator << (ostream& os, const Rational& rat);

friend istream& operator >> (istream& is, Rational& rat);

private:

int teller, number;

void reduce();

int number(int, int);

};

Rational::Rational(int t, int n): teller(t), number(n) {

reduce();

}

Rational Rational::operator -() const {

return Rational(-teller, number);

}

// operator overloading

Rational operator + (const Rational& lVal, const Rational& rVal) {

Rational result;

result.teller = lVal.teller * rVal.number + lVal.number * rVal.teller;

result.number = lVal.number * rVal.number;

result.reduce();

return result;

}

Rational operator - (const Rational& lVal, const Rational& rVal) {

Rational result;

result = lVal +- rVal;

result.reduce();

return result;

}

Rational operator * (const Rational& lVal, const Rational& rVal) {

Rational result;

result.teller = lVal.teller * rVal.teller;

result.number = lVal.number * rVal.number;

result.reduce();

return result;

}

Rational operator / (const Rational& lVal, const Rational& rVal) {

Rational result;

result.teller = lVal.teller * rVal.number;

result.number = lVal.number * rVal.teller;

result.reduce();

return result;

}

Rational& operator += (const Rational& rVal) {

teller = teller * rVal.number + number * rVal.teller;

number = number * rVal.number;

reduce();

return *this;

}

Rational& operator -= (const Rational& rVal) {

this->operator += (-rVal);

reduce();

return *this;

}

Rational& operator *= (const Rational& rVal) {

teller = teller * rVal.teller;

number = number * rVal.number;

reduce();

return *this;

}

Rational& operator /= (const Rational& rVal) {

teller = teller * rVal.number;

number = number * rVal.teller;

reduce();

return *this;

}

int operator < (const Rational& lVal, const Rational& rVal) {

return(lVal.toDouble() < rVal.toDouble());

}

int operator <= (const Rational& lVal, const Rational& rVal) {

return(lVal < rVal || lVal == rVal);

}

int operator == (const Rational& lVal, const Rational& rVal) {

return lVal.teller * rVal.number == lVal.number * rVal.teller;

}

int operator != (const Rational& lVal, const Rational& rVal) {

return !(lVal == rVal);

}

int operator >= (const Rational& lVal, const Rational& rVal) {

return(lVal > rVal || lVal == rVal);

}

int operator > (const Rational& lVal, const Rational& rVal) {

return(lVal.toDouble() > rVal.toDouble());

}

istream& operator >> (istream& is, Rational& rat) {

int t, n;

char deel;

cin >> is >> t >> deel >> n;

rat = Rational(t,n);

return is;

}

#endif

********************

// main.cpp

#include "rat.h"

#include <iostream>

#include <cstring>

#include <cstdlib>

using namespace std;

int main() {

Rational r;

r.clrscr();

r.Rational b1;

r.Rational b2;

cout << "Input the number in the form (a/b): ";

cin >> b1 >> b2;

// Test for +, -, *, /

cout << b1 << " + " << b2 << " = " << b1 + b2 << endl;

cout << b1 << " - " << b2 << " = " << b1 - b2 << endl;

cout << b1 << " * " << b2 << " = " << b1 * b2 << endl;

cout << b1 << " / " << b2 << " = " << b1 / b2 << endl;

// test program for overloaded operators

cout << b1 << " < " << b2;

(b1 < b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " <= " << b2;

(b1 <= b2) ? cout << " [X]" << endl << endl : cout << " [ ]" << endl << endl;

cout << b1 << " == " << b2;

(b1 == b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " != " << b2;

(b1 != b2) ? cout << " [X]" << endl << endl : cout << " [ ]" << endl << endl;

cout << b1 << " >= " << b2;

(b1 >= b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " > " << b2;

(b1 > b2) ? cout << " [X]" << endl : cout << " [ ]" << endl << endl;

getch();

return 0;

}

*************

output = multiple errors.

3 Comments

1) You have declared a data member 'number' and a function member 'number', change one

2) At Rational operator - (const Rational& lVal, const Rational& rVal) implementation, this line is incorrect: result = lVal +- rVal; (I think must be result = lVal - rVal)

3) You are implementing a function you have not declared, unary minus operator: Rational Rational::operator -() const, declare it as: Rational operator -() const;

4) Many operators as Rational& operator += (const Rational& rVal), must be declared as class function members, like:

5) At operator >>, remove 'cin>>', it is not necessary

#include <iostream>

using namespace std;

to rat.h it was 38 errors. Actually, I think, you might have found that error yourself.

>>>> int number(int int);

That is the next compiler error, that simply says that you couldn't name a data member same as a function. But cause you don't need the function, you could remove that line ==> 37 errors.

>>>> Rational Rational::operator -() const {

Here, my compiler says that operator- isn't a member of class Rational and it is right. You have to add the prototype of that member function to the class definition ==> 33 errors

>>>> Rational& operator += (const Rational& rVal) {

>>>> Rational& operator -= (const Rational& rVal) {

>>>> Rational& operator *= (const Rational& rVal) {

>>>> Rational& operator /= (const Rational& rVal) {

These are member functions but you didn't define the class scope Rational:: prior to the function name, e. g.

Rational& Rational::operator += (const Rational& rVal) {

==> 13 errors

>>>> return(lVal.toDouble() < rVal.toDouble());

member toDouble wasn't defined yet. Add

double toDouble() const { return ((double)teller) / number; }

to class definition ==> 9 errors

>>>> cin >> is >> t >> deel >> n;

remove cin >> (?????) and it compiles... ==> 8 errors

>>>> Rational r;

>>>> r.clrscr();

>>>> r.Rational b1;

>>>> r.Rational b2;

change it to

Rational r;

// r.clrscr(); // clrscr *never* should be a member of class Rational

Rational b1;

Rational b2;

==> 1 error

>>>> getch();

You would need old <conio.h> for getch() but you also could do that:

char dummy;

cin >> dummy;

VoilĂ : 0 compiler errors, but two linker errors cause of missing operator<< und reduce function.

Regards, Alex

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Course of the Month11 days, 22 hours left to enroll

Join the community of 500,000 technology professionals and ask your questions.