c++ - 无序集 : invalid operands to binary expression ('const Play' and 'const Play' )

标签 c++ unordered-set

当我尝试将元素插入 unordered_set 时出现此错误:

error: invalid operands to binary expression ('const Play' and 'const Play')
        {return __x == __y;}

这是整个错误的截图: https://www.dropbox.com/s/nxq5skjm5mvzav3/Screenshot%202013-11-21%2020.11.24.png

这是我的哈希函数:

struct Hash {

        size_t operator() (Play &play) {

            unsigned long hash = 5381;
            int c;

            string s_str = play.get_defense_name() + play.get_offense_name() + play.get_description();
            const char * str = s_str.c_str();

            while ( (c = *str++) )
                hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

            cout << hash << endl;
            return hash;

       }
    };

这是我声明 unordered_list 的地方:

unordered_set<Play, Hash> Plays;

这是我为我的 Play 类重载的 ==:

friend bool operator== (Play& p1, Play& p2)
    {
        return 
            (p1.get_defense_name() == p2.get_defense_name()) && 
            (p1.get_offense_name() == p2.get_offense_name()) &&
            (p1.get_description() == p2.get_description()); 
    }

知道这里会发生什么吗?

谢谢。

最佳答案

错误是说它尝试将 const Playconst Play 进行比较,但您只为 Play< 提供 operator ==/

friend bool operator== (const Play& p1, const Play& p2)
    {
        return 
            (p1.get_defense_name() == p2.get_defense_name()) && 
            (p1.get_offense_name() == p2.get_offense_name()) &&
            (p1.get_description() == p2.get_description()); 
    }

关于c++ - 无序集 : invalid operands to binary expression ('const Play' and 'const Play' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20135153/

相关文章:

c# - 为什么 if( !A && !B ) 不能优化为单个 TEST 指令?

c++ - std::unordered_set 如何避免迭代中每个元素上潜在的页面错误?

c++ - std::unordered_set<Foo> 作为类 Foo 的成员

c++ - 如何在 C++ 中使用哈希函数找到两个数组的交集?

.net - C++ 托管和非托管静态库

c++ - bazel中cc_library的增量是多少

c++ - 别名 - clang 优化器害怕什么?

c++ - std::unordered_set 如何存在病理输入?

c++ - unordered_map 删除段错误

c++ - 专用于私有(private)成员类的 std::hash