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