2011-02-16 82 views
1

我有很簡單的和愚蠢的麻煩:的std ::地圖::插入麻煩

std::map<b2Vec2, b2Body*> mTakePoints; 
mTakePoints.insert(std::make_pair(point, body)); 

編譯器說:

In file included from /usr/include/c++/4.4/string:50, 
       from /usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_types.h:34, 
       from /usr/include/ClanLib-2.2/ClanLib/Display/display.h:35, 
       from /usr/include/ClanLib-2.2/ClanLib/display.h:40, 
       from /home/pfight/Themisto/include/World/Actions/Action.hpp:21, 
       from /home/pfight/Themisto/include/World/Actions/TakeAction.hpp:21, 
       from /home/pfight/Themisto/src/World/Actions/TakeAction.cpp:18: 
/usr/include/c++/4.4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = b2Vec2]’: 
/usr/include/c++/4.4/bits/stl_tree.h:1170: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = b2Vec2, _Val = std::pair<const b2Vec2, b2Body*>, _KeyOfValue = std::_Select1st<std::pair<const b2Vec2, b2Body*> >, _Compare = std::less<b2Vec2>, _Alloc = std::allocator<std::pair<const b2Vec2, b2Body*> >]’ 
/usr/include/c++/4.4/bits/stl_map.h:500: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std::pair<const _Key, _Tp>&) [with _Key = b2Vec2, _Tp = b2Body*, _Compare = std::less<b2Vec2>, _Alloc = std::allocator<std::pair<const b2Vec2, b2Body*> >]’ 
/home/pfight/Themisto/src/World/Actions/TakeAction.cpp:43: instantiated from here 
/usr/include/c++/4.4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’ 
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data16.h:383: note: candidates are: bool operator<(const CL_StringData16&, const wchar_t*) 
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data16.h:382: note:     bool operator<(const wchar_t*, const CL_StringData16&) 
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data16.h:381: note:     bool operator<(const CL_StringData16&, const CL_StringData16&) 
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data8.h:383: note:     bool operator<(const CL_StringData8&, const char*) 
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data8.h:382: note:     bool operator<(const char*, const CL_StringData8&) 
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data8.h:381: note:     bool operator<(const CL_StringData8&, const CL_StringData8&) 

TakeAction.cpp:43它與插入通話行。爲了好玩,我試了下:

std::pair<b2Vec2, b2Body*> item(point, body); 
mTakePoints.insert(item); 

都一樣。
我很困惑,請解釋一下,有什麼不對?

回答

4

您是否定義了以下操作符:?

bool operator< (const b2Vec2&, const b2Vec2&) 

std::map要求-小於操作者對鍵來定義,或一個小於運算符用作模板參數來提供。特別地,std::map被實現爲(排序的)二叉樹,並且需要運營商<找出在哪裏放置新項目。

1

對於地圖,集合等有序容器必須告知放置新項目的位置。這是通過實施完成的小於操作:

bool operator< (const b2Vec2& first, const b2Vec2& second) 
{ 
    return first.some_attribute < second.some_attribute; 
} 

您可以在編譯錯誤看到它缺少:

/usr/include/c++/4.4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’ 

輸出的其餘部分告訴你,編譯器試圖找到一個合適的操作員並失敗。