【发布时间】:2017-05-10 17:43:38
【问题描述】:
我来自 Python/Java 领域,现在我正在尝试用 C++ 制作自己的哈希图。
当我尝试检查以将哈希表中的位置位置与 NULL 进行比较时(第 6 行和第 11 行),我收到错误:
invalid operands to binary expression ('value_type' (aka 'HashEntry') and 'long')"
我希望有人能引导我了解我做错了什么以及如何解决它。
void CustomHash::insert(HashEntry entry) {
int k = entry.getKey();
int i = 0;
int hashVal = hash_One(k);
int temp = hashVal;
while (i < size && hashArray[temp] != NULL) {
i++;
temp = (hashVal + i*hash_Two(k)) % size;
}
if (hashArray[temp] == NULL) {
hashArray[temp] = entry;
}
else {
cout << "Failure" << endl;
}
}
编辑 1:hashArray 声明
CustomHash::CustomHash(int m) {
this->size = m;
this->hashArray = vector<HashEntry>();
}
【问题讨论】:
-
编译器告诉你这是一个错误。如果您出示
hasArray的声明,也许有人可以提供解决方案。 -
hashArray.count(temp) -
@Justin André 正在编写自己的哈希图作为练习。可以合理地假设
hashArray属于该类型而不是标准哈希映射。因此,您不能假设它具有count方法。 -
不要在现代 C++ 中使用
NULL- 我们现在有nullptr。 -
@RSahu hashArray 是一个向量
对象
标签: c++ vector null comparison