error: no match for 'operator<' (operand types are 'const Request_Info' and 'const Request_Info')xxxxxxx

created at 10-20-2021 views: 2

code

Request_Info requestInfo;
    requestInfo.askTYpe = askType;
    requestInfo.askName = _getAskName(askType, jsonStr);
    if(m_askIdMap.count(requestInfo) < 1){ //Compile this code and report an error
        std::cout << "no match request:" << askType << "," << jsonStr;
    }else {
    }

Cause Analysis:

When the std::map.count() function is executed, the size of the key will be compared. As a custom type Request_Info, the size comparison itself cannot be done.

solution:

  1. Change to a type that can be compared in size to make the key value of the map.
  2. Add the < operator overload function to the custom type, as shown below:
struct Request_Info{
     std::string askTYpe; //Request, success, failure
     std::string askName; //SelectFilebyMultiKey, ShowRootDirectory, etc.
     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;
     }
};
created at:10-20-2021
edited at: 10-20-2021: