-3

Have this code in a simple class Time composed of two integers (day/hour) for an arduino project.

bool operator <= (const Time& other) const{
    return (day <= other.day && hour <= other.hour);
}

Although I do have an almost identical (and working) other class which uses the same syntax with the same non-static form... I'm getting the following errors :

error: non-member function ‘bool operator<=(const Time&)’ cannot have cv-qualifier
error: ‘bool operator<=(const Time&)’ must take exactly two arguments

I'm afraid that I'm at a loss despite having spent the last half-hour googling similar errors. Thanks!

3 Answers3

3

You have a non-member comparison operator. It makes sense for it to be a non-member rather than a member, but it requires two arguments, and cannot be const:

bool operator <= (const Time& lhs, const Time& rhs) { .... }

Also, you need to fix the logic of the comparison. I am very lazy so I would do it like this:

#include <tuple>

bool operator <= (const Time& lhs, const Time& rhs)
{
  return std::tie(lhs.day, lhs.hour) <= std::tie(rhs.day, rhs.hour);
}

See more on operator overloading here.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1
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.

Community
  • 1
  • 1
0

If this operator was declared as a class member function then you need to define it the following way

bool Time::operator <= (const Time& other) const{
    return (day <= other.day && hour <= other.hour);
}

Also take into account that the condition inside the operator is invalid. There should be

bool Time::operator <= (const Time& other) const{
    return ( day <= other.day ) && ( other.day > day || hour <= other.hour ) );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335