免费注册 查看新帖 |

Chinaunix

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

[C++] 出学者紧急求助:关于c++ stl--map的使用问题, [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-28 14:03 |只看该作者 |倒序浏览
#include "stdafx.h"
#include <utility>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

struct Item
{
        string name;
        string cd;
   
        Item(const string &name,const string &cd)
        {
                this->name=name;
                this->cd=cd;
        }

        bool operator==( const Item & object)
        {
                return this->cd == object.cd;
        }

        bool operator<( const Item & object)
        {
                return this->cd < object.cd;
        }
};

struct Area
{
        string name;
        string cd;

        Area(const string &name,const string &cd)
        {
                this->name=name;
                this->cd=cd;
        }

        bool operator==( const Area & object)
        {
                return this->cd == object.cd;
        }

        bool operator<( const Area & object)
        {
                return this->cd < object.cd;
        }
};



int _tmain(int argc, _TCHAR* argv[])
{
        map< Item,int > ItemPosition;

        map< Area, vector<string> > ResultMap;

        map< Area, vector<int> > AreaFlag;
    //
    vector<int> aa;
    Area bb("ss","sss");
        AreaFlag.insert(make_pair(bb,aa));

        return 0;
}

错误提示:
1        error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Area' (or there is no acceptable conversion)        d:\microsoft visual studio 8\vc\include\functional        143       

请各位前辈赐教,谢谢

论坛徽章:
0
2 [报告]
发表于 2007-06-28 14:09 |只看该作者
Item 操作符不能在 const Item 对象/引用上调用

论坛徽章:
0
3 [报告]
发表于 2007-06-28 14:13 |只看该作者
确实疏漏了,多谢前辈!

[ 本帖最后由 谢灵运 于 2007-6-28 14:17 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2007-06-28 15:30 |只看该作者
原帖由 wolf0403 于 2007-6-28 14:09 发表
Item 操作符不能在 const Item 对象/引用上调用

呵呵,高人说话总是这么羞涩难懂。

第一次见到这种问题(有点孤陋寡闻)

g++ 会给出下面的错误提示, 第一行内容如下(节选)
In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Area]’:


而你的是
bool operator<( const Area & object)


少了一个 const, 改成下面这种形式就OK了
bool operator<( const Area & object) const

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2007-06-28 17:15 |只看该作者
两种都要定义吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP