1. 程式人生 > 其它 >error: no match for 'operator<' (operand types are 'const Request_Info' and 'const Request_Info')xxxxxxx

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; } };
堅持成就偉大