Click to See Complete Forum and Search --> : STL and operator< (for the less<>function)


August 27th, 2001, 01:54 PM
I have defined a class T with 2 attributes, overloaded the operator&lt; , and used this as the first parameter in an the stl multimap.
The problem is the compiler complains that stl can't handle the overloaded operator&lt;. Some error about using the this pointer
and how it doesn't qualify as a parameter in the stl function.h

I hope you know better than I what that all means, because I was sure that operator overloading was necessary for user defined classes used as the key in a multimap and using the less&lt;&gt; function to do insertions into it.

Please email me any suggestions at: thewarman@yahoo.com

Igor Soukhov
August 27th, 2001, 03:46 PM
Hi ! It's not very clear what errors you was getting, but define "operator&lt;()" in your class as in this example. Defining of "operator&lt;" is not only way to use your class as the key for the multimap - but I'm sure - the simplest one... Here is the example:


struct MyKeyData
{
int iValue;
double dValue;

bool operator&lt;(MyKeyData const& rhs) const
{
return iValue &lt; rhs.iValue;
}
};




also you might define "operator&lt;" just as the non-member function (I prefer this way because
it's sipmlifying the interface of the class and for other minor reasons):

bool operator&lt;(MyKeyData const& key1, MyKeyData const& key2)
{
return key1.iValue &lt; key2.iValue;
}




Please - rate answer if it helped you
It gives me inspiration when I see myself in the top list =)

Best regards,

-----------
Igor Soukhov (Brainbench/Tekmetrics ID:50759)
igor@soukhov.com | ICQ:57404554 | http://soukhov.com

Member of Russian Software Developer Network http://rsdn.ru