当我尝试将元素插入到unordered_set时,我收到此错误:
error: invalid operands to binary expression ('const Play' and 'const Play')
{return __x == __y;}
Run Code Online (Sandbox Code Playgroud)
以下是整个错误的屏幕截图: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;
}
};
Run Code Online (Sandbox Code Playgroud)
这是我声明unordered_list的地方:
unordered_set<Play, Hash> Plays;
Run Code Online (Sandbox Code Playgroud)
这是==我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());
}
Run Code Online (Sandbox Code Playgroud)
知道这可能会发生什么吗?
谢谢.
错误是说它试图比较const Play有const Play,但你只提供operator ==了Play
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());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3962 次 |
| 最近记录: |