我目前正在为一个类项目工作,在该项目中我必须在 C++ 中实现布谷鸟散列。问题是,我和 C++ 从来都不是朋友,我认为我们永远不会…
具体问题是,我无法在已经存在的对象上设置指针。当我这样做时,我得到编译错误:
从 ‘const std::__1::basic_string 到 ‘std::__1::basic_string ‘* 没有可行的转换
两个语句都会出现错误:
E * activeE = e;
E * tempE = v1[pos];
v1 是一个 E 对象数组。
我认为这个错误是由于我对 C++ 基本概念的普遍误解造成的。我认为对你们来说这个问题是一个笑话,但我还是希望你们能帮助我。
template <typename E, size_t N>
void Hashing<E,N>::add_(const E& e) {
size_t pos = h1(e);
size_t i = 0;
E * activeE = e;
E * tempE = v1[pos];
while (i < nmax) {
if (tempE == NULL) {
v1[pos] = activeE;
break;
}
v1[pos] = activeE;
activeE = tempE;
pos = h2(activeE);
tempE = v2[pos];
if (tempE == NULL) {
v2[pos] = activeE;
break;
}
v2[pos] = activeE;
activeE = tempE;
pos = h1(activeE);
tempE = v1[pos];
}
}
原文由 backfloep 发布,翻译遵循 CC BY-SA 4.0 许可协议
You have
const E& e
inHashing<E,N>::add_
method, but inside of it you assigne
to pointer toE
- actually this should generate different error:所以修复,因为这是改变:
至