zoukankan      html  css  js  c++  java
  • error: no match for 'operator<' (operand types are 'const Request_Info' and 'const Request_Info')xxxxxxx

    代码:

       Request_Info requestInfo;
        requestInfo.askTYpe = askType;
        requestInfo.askName = _getAskName(askType, jsonStr);
        if(m_askIdMap.count(requestInfo) < 1){ //编译此代码报错
            std::cout << "no match request:" << askType << "," << jsonStr;
        }else {
        }

    原因分析:

    执行std::map.count()函数的时候会对key的大小做比较,作为自定义类型Request_Info,本身无法做大小比较。

    解决方案:

    1.换一个能够大小比较的类型做map的key值。

    2.为自定义类型增加<操作符重载函数,如下所示:

        struct Request_Info{
        std::string     askTYpe;            //请求、成功、失败
        std::string     askName;            //SelectFilebyMultiKey、ShowRootDirectory等
        bool operator<(const struct Request_Info& requestInfo){
            bool ret = false;
            if(askName<requestInfo.askName){
                ret = true;
            }else if(askName == requestInfo.askName){
                if(askTYpe < requestInfo.askTYpe){
                    ret = true;
                }
            }
            return ret;
        }
        friend bool operator<(const struct Request_Info& req1, const struct Request_Info& req2){
            bool ret = false;
            if(req1.askName<req2.askName){
                ret = true;
            }else if(req1.askName == req2.askName){
                if(req1.askTYpe < req2.askTYpe){
                    ret = true;
                }
            }
            return ret;
        }
    };
    坚持成就伟大
  • 相关阅读:
    江西财经大学第一届程序设计竞赛 I 题 小P和小Q
    江西财经大学第一届程序设计竞赛 H题- 小P的数学问题
    C# 窗体
    数据库操作(对战游戏)
    数据库操作 (数据操作类)
    练习
    泛型集合
    数据库操作 (防注入攻击)
    数据库操作(增删改)
    DO.NET操作数据库
  • 原文地址:https://www.cnblogs.com/xian-yongchao/p/15406472.html
Copyright © 2011-2022 走看看