0

I'm stuck trying to figure out how to overload the << operator so I can display my rational class functions.

These are the lines I'm trying to overload:

cout << "Testing the compare() member function, found:" << endl;
if (r1.compare(r2) == 1)
  cout << r1.display() << " is greater than " << r2.display() << endl;
else if (r1.compare(r2) == 0)
  cout << r1.display() << " is equal to " << r2.display() << endl;
else if (r1.compare(r2) == -1)
  cout << r1.display() << " is less than " << r2.display() << endl;

cout << "Testing the four arithmetic member functions:" << endl;
cout << "r1 + r2 = " << r1.display() << " + " << r2.display() << " = " << r1.add(r2) << endl;
cout << "r1 - r2 = " << r1.display() << " - " << r2.display() << " = " << r1.subtract(r2) << endl;
cout << "r1 * r2 = " << r1.display() << " * " << r2.display() << " = " << r1.multiply(r2) << endl;
cout << "r1 / r2 = " << r1.display() << " / " << r2.display() << " = " << r1.divide(r2) << endl;

The errors occur everytime I call a function. Here is the code for the functions:

void rational::display()
{
   int gcd = GCD();
   if (denom < 0)
   {
      num = -num;
      cout << num / gcd << " / " << denom / gcd << endl;
   }
   else if (num == 0)
   cout << num << endl;
}

rational rational::add(const rational &r2) const
{
   rational sum; 
   sum.num = (num * r2.denom) + (r2.num * denom);
   sum.denom = denom * r2.denom;
   return sum;
}

The multiply, divide, and subtract functions are as same as the add they just have the symbols and variable name changed to match the operation. My overload operator is set up like this:

ostream& operator<< (ostream& out, rational &robj)
{
   out << example code << example code;
   return out;
}

Any help would be appreciated. This is my first time posting so if I need to post more of my code I will. Thanks!

LunyJake
  • 3
  • 2
  • 1
    What errors occur? Be as specific as possible. – Scott Hunter Nov 18 '19 at 23:14
  • So for this line: cout << r1.display() << " is greater than " << r2.display() << endl; I get an error like this: [Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'void') – LunyJake Nov 18 '19 at 23:21
  • Not sure of your error, but try `const rational &robj` in your stream operator. – lakeweb Nov 18 '19 at 23:52

1 Answers1

1

First of all, change

ostream& operator<< (ostream& out, rational &robj)

to

ostream& operator<< (ostream& out, rational const& robj)

Then, instead of

cout << r1.display() << " is greater than " << r2.display() << endl;

use

cout << r1 << " is greater than " << r2 << endl;

Since you already have the above operator<< function, I would get rid of the member function display(). It doesn't (should not) give you any more functionality than using the operator<< function.

Given

rational 1

Use of

r.display();

should give you the same output as

cout << r << endl;

See What are the basic rules and idioms for operator overloading? for more details on operator overloading.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I got it to work but I have a function that has a variable passed through like this: r1.add(r2). How would I define this in the >> overload? Putting r1 (r2) in the main gives me an error. – LunyJake Nov 19 '19 at 03:07
  • @LunyJake, Glad to hear you got thinks working. I think it will be better to create another post for such a question. – R Sahu Nov 19 '19 at 04:08