CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    STL and operator< (for the less<>function)

    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: [email protected]


  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: STL and operator< (for the less<>function)

    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)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Member of Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured