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;
        }
    };
    坚持成就伟大
  • 相关阅读:
    JS的IE和FF兼容性问题汇总
    解决flash挡住层的问题
    javascript 代码优化工具 UglifyJS
    理解面向对象
    js中的等号与非等号
    js 的数据类型转换
    js优化 ----js的有序加载
    各浏览器对页面外部资源加载的策略
    js 执行效率
    脚本的加载,解析,与执行
  • 原文地址:https://www.cnblogs.com/xian-yongchao/p/15406472.html
Copyright © 2011-2022 走看看