【问题标题】:operator< for user defined key of map - C2679运算符 < 用于地图的用户定义键 - C2679
【发布时间】:2018-05-14 09:42:52
【问题描述】:

我正在尝试创建一个类似的地图

std::map<std::map <std::string,std::string>, MyDataTyp*>, 

所以我放了

std::map<std::string, std::string> 

在一个单独的类中,例如:

class TagList {
public:
std::map<std::string, std::string> _map;

TagList() {}
~TagList() {}
void addTag(const std::string tag, const std::string value) { if (tag != "") _map[tag] = value; }
std::string getValue(const std::string tag) {
    std::map<std::string, std::string>::iterator it = _map.find(tag);
    if (it == _map.end()) return ("");
    else return (it->second);
}

};

inline bool operator< (const TagList &a, const TagList &b) {

std::map<std::string, std::string>::iterator it ;
for (it = a._map.begin(); it != a._map.end(); it++) {
    std::string myVal1 = it->second;
    std::string myVal2 = b._map.find(it->first);
    if (myVal2 == "") return false;
    if (strcmp(myVal1.c_str(),myVal2.c_str()) > 0) return true;

}
return false;

}

希望有人能解释一下错误信息

错误 4 错误 C2679:二进制“=”:未找到采用“std::_Tree_const_iterator<_mytree>”类型右侧操作数的运算符(或没有可接受的转换)(...)

……我的告白的原因是什么……

【问题讨论】:

    标签: visual-c++ operator-keyword stdmap


    【解决方案1】:

    对不起,是我的错……

    自己找到了答案:

    inline bool operator< (const TagList &a, const TagList &b) {
    
    std::map<std::string, std::string>::const_iterator it ;
    std::map<std::string, std::string>::const_iterator it2 ;
    for (it = a._map.begin(); it != a._map.end(); it++) {
        std::string myVal1 = it->second;
        it2 = b._map.find(it->first);
        std::string myVal2 = it2->second;
        if (myVal2 == "") return false;
        if (strcmp(myVal1.c_str(),myVal2.c_str()) > 0) return true;
    
    }
    return false;
    }
    

    不是常量

    谢谢 乔治

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-04
      • 1970-01-01
      • 2020-10-02
      • 2022-01-11
      • 2013-11-18
      • 1970-01-01
      相关资源
      最近更新 更多