C++ STL 中的map容器中的元素是按照Key的升序进行排列的,所以map的Key需要使用自定义类型时,该自定义类型必须重载operator<操作符,否则会出现下面的编译错误:
-
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/string:50,
-
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/locale_classes.h:42,
-
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ios_base.h:43,
-
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ios:43,
-
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:40,
-
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iostream:40,
-
from dagexing/src/online/dgxdbrpc/dgx_db.cc:2:
-
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_function.h: In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = dgx_db::KtvRoomInfo]':
-
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:449: instantiated from '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = dgx_db::KtvRoomInfo, _Tp = dgx_db::Uint32DataType, _Compare = std::less, _Alloc = std::allocator >]'
-
./dagexing/src/online/dgxdbrpc/dbcache.h:53: instantiated from 'int dgx_db::DbCache::update(const KeyType&, ValueType&) [with KeyType = dgx_db::KtvRoomInfo, ValueType = dgx_db::Uint32DataType]'
-
dagexing/src/online/dgxdbrpc/dgx_db.cc:136: instantiated from here
-
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_function.h:230: error: no match for 'operator<' in '__x < __y'
这个错误表示_Key = dgx_db::KtvRoomInfo没有重载operator<操作符。加入下列重载函数后通过编译
-
bool operator < (const KtvRoomInfo& info) const {
-
if (kiid_ < info.kiid_)
-
return true;
-
else if (kiid_ == info.kiid_ && enname_ < info.enname_)
-
return true;
-
else
-
return false;
-
}
阅读(7491) | 评论(0) | 转发(0) |