解决办法 error: no match for ‘operator<’ (operand types are ‘const Ecu’ and ‘const Ecu’) 386 | {

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,本身无法做大小比较。

这个错误是由于你没有为Ecu类重载小于号运算符,而std::sort函数需要使用这个运算符来比较Ecu对象的大小。你需要在Ecu类中定义一个operator<函数,该函数接收一个const Ecu对象作为参数,然后根据ecuID_的值来返回一个布尔值,表示当前对象是否小于参数对象。具体的代码如下:

解决方案:

1.换一个能够大小比较的类型做map的key值。

2.为自定义类型增加<操作符重载函数,如下所示:

// 重载小于号运算符
bool operator<(const Ecu& other) const {
    // 比较ecuID_的值,如果小于,返回true;否则返回false
    return ecuID_ < other.ecuID_;
}


(1)https://www.cnblogs.com/xian-yongchao/p/15406472.html

(2)c++ - error: no match for ‘operator<’ (operand types are ‘const A’ and .... https://stackoverflow.com/questions/68626001/error-no-match-for-operator-operand-types-are-const-a-and-const-a.
(3) No Match for 'operator' error in C++ - Stack Overflow. https://stackoverflow.com/questions/22411514/no-match-for-operator-error-in-c.
(4) C++ Overload: [Error] no match for 'operator=' (operand types are .... https://stackoverflow.com/questions/45371349/c-overload-error-no-match-for-operator-operand-types-are-string-and.
(5) 【c++】解决使用 std::map 时报错 no match for ‘operator<’-CSDN博客. https://blog.csdn.net/weixin_43667077/article/details/133635409.
(6) undefined. http://ideone.com/Ecm6I9.

你可能感兴趣的:(c++,c++)