map的编译问题
#include<map>#include<string>#include<iostream>usingnamespacestd;classKey{public:stri...
#include <map>
#include <string>
#include <iostream>
using namespace std;
class Key
{
public:
string k;
};
int main(int argc, char* argv[])
{
Key k;
pair<Key, string> p(k, "world");
map<Key, string> m;
m.insert(p);
}
在insert那行编译报错,注释掉就不报错了:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h: In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Key]':
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:921: instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_unique(const _Val&) [with _Key = Key, _Val = std::pair<const Key, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _KeyOfValue = std::_Select1st<std::pair<const Key, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<Key>, _Alloc = std::allocator<std::pair<const Key, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_map.h:396: 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 = Key, _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::less<Key>, _Alloc = std::allocator<std::pair<const Key, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]'
a.cpp:18: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:227: error: no match for 'operator<' in '__x < __y' 展开
#include <string>
#include <iostream>
using namespace std;
class Key
{
public:
string k;
};
int main(int argc, char* argv[])
{
Key k;
pair<Key, string> p(k, "world");
map<Key, string> m;
m.insert(p);
}
在insert那行编译报错,注释掉就不报错了:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h: In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Key]':
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:921: instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_unique(const _Val&) [with _Key = Key, _Val = std::pair<const Key, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _KeyOfValue = std::_Select1st<std::pair<const Key, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<Key>, _Alloc = std::allocator<std::pair<const Key, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_map.h:396: 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 = Key, _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::less<Key>, _Alloc = std::allocator<std::pair<const Key, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]'
a.cpp:18: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:227: error: no match for 'operator<' in '__x < __y' 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询