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;
    }
};

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM