免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1437 | 回复: 2
打印 上一主题 下一主题

一个map的错误,大家看看。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-07-16 09:37 |只看该作者 |倒序浏览
以下是程序:

  1. #include <stdio.h>;
  2. #include <string.h>;
  3. #include <map>;
  4. using namespace std;
  5. struct aaa
  6. {
  7.         int b;
  8.        
  9.         bool operator <(const aaa &a)
  10.         {
  11.                 if(b<a.b)
  12.                         return true;
  13.                 return false;
  14.         };

  15. };
  16. typedef map<aaa,int>; tmap;
  17. int main()
  18. {
  19.         tmap m_tmap;
  20.         aaa a;
  21.         aaa b;
  22.         a.b=1;
  23.         m_tmap.find(a);
  24.         return 0;
  25. }
复制代码

编译错误:
[king@e2p testcode]$ g++ -o test test.cpp
/usr/include/g++-3/stl_function.h: In method `bool
less<_Tp>;:perator() (const _Tp &, const _Tp & const [with _Tp =
aaa]':
/usr/include/g++-3/stl_tree.h:1121:   instantiated from `_Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>;::find (const _Key & [with _Key = aaa, _Value = pair<const aaa, int>;, _KeyOfValue = _Select1st<pair<const aaa, int>; >;, _Compare = less<aaa>;, _Alloc = allocator<int>;]'
/usr/include/g++-3/stl_map.h:187:   instantiated from `map<_Key, _Tp, _Compare, _Alloc>;::find (const _Key & [with _Key = aaa, _Tp = int, _Compare = less<aaa>;, _Alloc = allocator<int>;]'
test.cpp:24:   instantiated from here
/usr/include/g++-3/stl_function.h:111: passing `const aaa' as `this'
argument of `bool aaa:perator< (const aaa &' discards qualifiers

大家看看是哪里有问题啊!!

论坛徽章:
0
2 [报告]
发表于 2003-07-16 15:36 |只看该作者

一个map的错误,大家看看。

编译不过是应为m_tmap.find(a); 有错误,正确的如下所示
#include <stdio.h>;
#include <string.h>;
#include <map>;
using namespace std;

struct AAA
{
  int b;

  bool operator <(const AAA &a)
  {
     if(b<a.b)
        return true;
     return false;
  };
};

typedef std::map<unsigned long, AAA>; tmap;

int main()
{
tmap m_tmap;
AAA a;
AAA b;
a.b=1;
m_tmap[1]=a;
m_tmap[2]=b;

tmap::iterator ie;
ie = m_tmap.find(1);
if(ie != m_tmap.end())
{
     m_tmap.erase( ie );
}
return 0;
}

论坛徽章:
0
3 [报告]
发表于 2003-08-02 22:23 |只看该作者

一个map的错误,大家看看。

问题解决了。
把:
bool operator <(const aaa &a)
改成:
bool operator <(const aaa &a)  const
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP