std::map::插入麻烦

发布于 2024-10-17 19:57:32 字数 3291 浏览 5 评论 0原文

我有非常简单和愚蠢的麻烦:

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);

都一样。
我很困惑,请解释一下,哪里出了问题?

I have very simple and stupid trouble:

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

Compiler says:

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 it is row with insert call. For fun I tried next:

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

All the same.
I'm pretty confused, explain please, what is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话墙 2024-10-24 19:57:32

您是否定义了以下运算符:?

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

std::map 要求为键定义小于运算符,或者提供小于运算符作为模板参数。特别是,std::map 被实现为(排序的)二叉树,并且需要运算符<;找出放置新物品的位置。

Did you define the following operator:?

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

std::map requires that the less-than operator be defined for keys, or that a less-than operator be provided as a template argument. In particular, std::map is implemented as a (sorted) binary tree, and needs operator< to figure out where to put new items.

驱逐舰岛风号 2024-10-24 19:57:32

对于地图、集合等有序容器,必须告知在何处放置新项目。这是通过实现小于运算符来完成的:

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’

输出的其余部分告诉您编译器尝试找到合适的运算符但失败了。

For map, set and the like ordered containers must be told where to put new items. This is done by implementing the less-than operator:

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

You can see in the compilation error that it was missing:

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

The rest of the output tells you that the compiler tried to find a suitable operator and failed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文