6

I want to use a std::map whose key and value elements are structures.

I get the following error: error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const GUID

I understand that I should overload operator < for that case, but the thing is I don't have access to the code of the structure I want to use (GUID structure in VC++).

Here's the code snippet:

//.h

#include <map>
using namespace std;

map<GUID,GUID> mapGUID;


//.cpp

GUID tempObj1, tempObj2;              
mapGUID.insert( pair<GUID,GUID>(tempObj1, tempObj2) );   

How to solve this problem?

4 Answers 4

10

You can define the comparison operator as a freestanding function:

bool operator<(const GUID & Left, const GUID & Right)
{
    // comparison logic goes here
}

Or, since in general a < operator does not make much sense for GUIDs, you could instead provide a custom comparison functor as the third argument of the std::map template:

struct GUIDComparer
{
    bool operator()(const GUID & Left, const GUID & Right) const
    {
        // comparison logic goes here
    }
};

// ...

std::map<GUID, GUID, GUIDComparer> mapGUID;
Sign up to request clarification or add additional context in comments.

6 Comments

when I write the way you said, I get errors, since operator< function should look like bool operator<(GUID& left, GUID& right); Even after this I get compiler error C2804: binary 'operator <' has too many parameters. I don't understand why, cause this function is not placed inside my class containing maps?
@kobac -- I think that @Matteo meant to say bool operator()(const std::pair<GUID, GUID> & Left, const std::pair<GUID, GUID> & Right)
The correct answer is bool operator()(const GUID &left, const GUID &right) const
Ops, I got a bit messed up with the second one; sorry, now it should be fixed. :S
Still needs operator(). Please correct it so the people searching for the same problem would have a complete solution. Grazie Matteo.
|
2

Any type you use as a key has to provide a strict weak ordering. You can supply a comparator type as a third template argument, or you can overload operator< for your type.

1 Comment

upvoted for specifying strict weak ordering. This is important as otherwise a naive attempt at implementing our own operator< probably won't order strictly enough. Mine didn't, until I learned this term elsewhere.
0

There is no operator < for GUIDS so you either have to provide the comparison operator or use a different key.

Comments

0

May be using class inherited from GUID in which you implement operator < would be a workaround for you?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.