"Operator 'operator' not implemented in type 'type' for arguments of the same type" error and STL maps.

By: mykle hoban

Abstract: Ridding yourself of errors when using an STL map with a class that overloads the < or > operator.

Question: I'm using STL maps and a class in which I've overloaded the < or > operator and I keep getting errors. ("E2093 Operator 'operator' not implemented in type 'type' for arguments of the same type"). How do I get rid of this?
Answer: Make sure your operator overload is declared as a friend, rather than a class member. The STL object cannot deal with the class-member operator.
Code: Example code showing the way to do it right.
#include <condefs.h>
#include <map>

using namespace std;

class fred
{
    public:
    fred(int a) : i(a) {}
    bool operator<(const fred &f) { return i < f.i; }       // this will not work - error E2093
    //friend bool operator<(const fred &f1, const fred &f2);       // this will work

    private:
    int i;
};

/*
bool operator<(const fred &f1, const fred &f2)
{
    return f1.i < f2.i;
}
*/

#pragma argsused
int main(int argc, char* argv[])
{
    map mymap;

    mymap[fred(0)] = 0;
    return 0;
}

Server Response from: ETNASC04