Chinaunix首页 | 论坛 | 博客
  • 博客访问: 492624
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1693
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-13 09:23
个人简介

前EMC高级软件工程师,现小米分布式存储码农,关注分布式存储,文件系统,Linux内核。微博: http://weibo.com/u/2203007022

文章分类

全部博文(17)

文章存档

2015年(1)

2014年(6)

2013年(10)

分类: C/C++

2013-09-15 23:02:37

C++ STL 中的map容器中的元素是按照Key的升序进行排列的,所以map的Key需要使用自定义类型时,该自定义类型必须重载operator<操作符,否则会出现下面的编译错误:

点击(此处)折叠或打开

  1. In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/string:50,
  2.                  from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/locale_classes.h:42,
  3.                  from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ios_base.h:43,
  4.                  from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ios:43,
  5.                  from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:40,
  6.                  from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iostream:40,
  7.                  from dagexing/src/online/dgxdbrpc/dgx_db.cc:2:
  8. /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]':
  9. /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 >]'
  10. ./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]'
  11. dagexing/src/online/dgxdbrpc/dgx_db.cc:136: instantiated from here
  12. /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<操作符。加入下列重载函数后通过编译

点击(此处)折叠或打开

  1. bool operator < (const KtvRoomInfo& info) const {
  2.     if (kiid_ < info.kiid_)
  3.       return true;
  4.     else if (kiid_ == info.kiid_ && enname_ < info.enname_)
  5.       return true;
  6.     else
  7.       return false;
  8.   }


阅读(7512) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~