error

closed account (i8bjz8AR)
I'm getting this error when i compile
1
2
error: ‘bool Rational::operator>=(Rational, Rational)’ must take exactly one argument
 bool operator>=(Rational r1, Rational r2);


in my .hpp file
 
bool operator>=(Rational r1, Rational r2);


and this is what i have in my .cpp file

1
2
3
4
//    - r1 >= r2
bool operator>=(Rational r1, Rational r2){
  return (r1 - r2).num() >= 0;
}
Last edited on
seems pretty clear you have too many arguments.
closed account (i8bjz8AR)
so is it a simple fix?
Here is an example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;
class Int
{
public:

    Int(const int param):_number(param){}
    bool operator >=(const Int &param);
    int _number = 0;
};


bool Int::operator>=(const Int &param)
{
    return _number >= param._number;
}

int main()
{
    Int first(5);
    Int second(12);

    if(second >= first)
        std::cout << "Greater " << std::endl;

    return 0;
}



note: the >= operator only takes one parameter.

an overloaded operator is like any other method. You need an object of the class to call the method.

So where is a class object on this line of code? if(second >= first)

the left hand argument is always considered the calling object.

this second >= first
is equivalent to

second.operator>=(first);
Topic archived. No new replies allowed.