I’m trying to use a simple structure as a map key:
class Foo{
.
.
.
struct index{
int x;
int y;
int z;
};
bool operator<(const index a, const index b);
.
.
.
}
And the function itslef:
bool Foo::operator<(const index a, const index b){
bool out = True;
if (a.x == b.x){
if (a.y == b.y){
if (a.z >= b.z) out = false;
}
else if(a.y > b.y) out = false;
} else if (a.x > b.x) out = false;
return out;
}
However, when I compile I get an error:
memMC.h:35: error: ‘bool Foo::operator<(Foo::index,
Foo::index)’ must take exactly one argument
As I understand this, the compilers wants to compare index to this Foo. How can I overload the operator then?
If you want to compare two indexes, move the overload inside the
indexstructure:If you want to compare a
Fooand anindex(I doubt that, but I’ll just put this here just in case), remove the second parameter, as it’s not needed:Note that you should pass the
indexparameter by reference, to prevent unnecesarry copying.EDIT: As Als correctly pointed out, this operator should be
const.