error: non-member function ‘bool operator<=(const Time&)’ cannot have cv-qualifier
You cannot add const qualifiers to a non member function.
Let me explain you why
The const after a member function guarantees that this function will not change any member variable of this
. Now what would a static function guarantee if you don't have any this
to refer to?
error: ‘bool operator<=(const Time&)’ must take exactly two arguments
There are 2 ways of overloading operators. You can make them member functions (without the static), but if you want to compare int
and Time
for example.. Lets write it out:
in your Time class, write out the following operator:
bool operator<=(int rhs) const {return this.minutes <= rhs;}
now, let us compare our data:
Time time;
int integer;
if(time <= integer) {} //This will compile
if(integer <= time) () //This will not compile
lets see why that is:
if(time <= integer) {} //Will expand to this:
if(time.operator<=(integer)) {}
now can you guess what this does?
if(integer <= time) {} //Will expand to this:
if(integer.operator<=(time)) {}
There is no such operator for a default type like integers, and you can't simply just add one to the integer type.
The solution to this are the static operators.
To cover both cases (int <= Time and Time <= int)
you can write this operator as well in your Time class
static bool operator<=(int lhs, const Time& rhs) {};
The parameters are the left side of the comparison and the right side of the comparison.
I also suggest you to take a look at juanchopanza's link:
See more on operator overloading here.
and this link for the difference between member function operators and non member function operators.