24

I am trying to assign a custom type as a key for std::map. Here is the type which I am using as key:

struct Foo
{
    Foo(std::string s) : foo_value(s){}

    bool operator<(const Foo& foo1) {   return foo_value < foo1.foo_value;  }

    bool operator>(const Foo& foo1) {   return foo_value > foo1.foo_value;  }
    
    std::string foo_value;
};

When used with std::map, I am getting the following error:

error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Foo' (or there is no acceptable conversion) c:\program files\microsoft visual studio 8\vc\include\functional 143

If I change the struct to the one below, everything works:

struct Foo
{
    Foo(std::string s) : foo_value(s)   {}

    friend bool operator<(const Foo& foo,const Foo& foo1) { return foo.foo_value < foo1.foo_value;  }

    friend bool operator>(const Foo& foo,const Foo& foo1) { return foo.foo_value > foo1.foo_value;  }
    
    std::string foo_value;
};

Nothing changed, except that the operator is overloaded as friend. Why does my first code not work?

honk
  • 9,137
  • 11
  • 75
  • 83
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184

3 Answers3

33

I suspect you need

bool operator<(const Foo& foo1) const;

Note the const after the arguments, this is to make "your" (the left-hand side in the comparison) object constant.

The reason only a single operator is needed is that it is enough to implement the required ordering. To answer the abstract question "does a have to come before b?" it is enough to know whether a is less than b.

Matt Davis
  • 45,297
  • 16
  • 93
  • 124
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Can you go into more detail? Why do you only need operator< and not operator== or operator>? – bobobobo Dec 06 '09 at 18:38
  • 14
    because you can derive operator> and operator== from operator<. `(b < a)` implies `(a > b)`, so there's operator>. and, `(!(a < b) && !(b < a))` means that a isn't less than b nor greater than b, so it must be equal to b. – skrebbel Aug 23 '10 at 08:39
3

It's probably looking for const member operators (whatever the correct name is). This works (note const):

bool operator<(const Foo& foo1) const { return foo_value < foo1.foo_value;}

EDIT: deleted operator> from my answer as it was not needed (copy/paste from question) but it was attracting comments :)

Note: I'm 100% sure that you need that const because I compiled the example.

stefanB
  • 77,323
  • 27
  • 116
  • 141
0

The other answers already solve your problem, but I'd like to offer an alternative solution. Since C++11 you can use a lambda expression instead of defining operator< for your struct. (operator> is not needed for your map to work.) Providing a lambda expression to the constructor of a map has certain advantages:

  • I find the declaration of a lambda expressions to be simpler and less error-prone than that of operators.
  • This approach is especially useful if you can't modify the struct that you want to store in your map.
  • You can provide different comparison functions for different maps that use your struct as key.
  • You can still define operator< differently and use it for a different purpose.

As a result, you can keep your struct as short as follows:

struct Foo {
    Foo(std::string s) : foo_value(s) {}
    std::string foo_value;
};

And your map can then be defined in the following way:

int main() {
    auto comp = [](const Foo& f1, const Foo& f2) { return f1.foo_value < f2.foo_value; };
    std::map<Foo, int, decltype(comp)> m({ {Foo("b"), 2}, {Foo("a"), 1} }, comp);
    // Note: To create an empty map, use the next line instead of the previous one.
    // std::map<Foo, int, decltype(comp)> m(comp); 

    for (auto const &kv : m)
        std::cout << kv.first.foo_value << ": " << kv.second << std::endl;

    return 0;
}

Output:

a: 1
b: 2

Code on Ideone

honk
  • 9,137
  • 11
  • 75
  • 83