[英]No viable conversion from 'const std::__1::basic_string<char> to 'std::__1::basic_string<char> *'
I'm currently working on an project for a class in which I've to implement cuckoo hashing in C++. 我目前正在为一个类的项目工作,该类必须在C ++中实现布谷鸟哈希。 The problem is, that C++ and I were never friends and I think we never will be...
问题是,我和C ++从来都不是朋友,我认为我们永远不会...
The concrete problem is, that I can't wangle to set an pointer on an already existing Object. 具体的问题是,我不能随意在已经存在的Object上设置指针。 When I do so, I get the compile error:
这样做时,出现编译错误:
No viable conversion from 'const std::__1::basic_string to 'std::__1::basic_string '* 从'const std :: __ 1 :: basic_string到'std :: __ 1 :: basic_string '* 没有可行的转换
The error occurs for both statements: 这两个语句均发生错误:
E * activeE = e;
E * tempE = v1[pos];
v1 is an array of E Objects. v1是E对象的数组。
I think this error is caused by my generally misunderstanding in the basic concept of C++. 我认为此错误是由于我对C ++基本概念的普遍误解引起的。 I think for you guys this problem is a joke, but I hope you help me anyway.
我认为对于你们来说,这个问题是个玩笑,但我还是希望您能为我提供帮助。
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];
}
}
I get the compile error:
我收到编译错误:
No viable conversion from 'const std::__1::basic_string to 'std::__1::basic_string"
无法从'const std :: __ 1 :: basic_string转换为'std :: __ 1 :: basic_string”
Are you sure? 你确定吗? I don't think that's what the error says.
我不认为那是错误的意思。 I bet it says:
我敢打赌说:
No viable conversion from 'const std::__1::basic_string to 'std::__1::basic_string*'
Note the extra *
which is very significant, and is the source of your problem. 请注意,额外的
*
非常重要,它是问题的根源。 You need to pay attention to the error messages, the details matter. 您需要注意错误消息,细节很重要。
A pointer is a variable that holds the address of something else, so to create a pointer to an object you need to use the "address of" operator, &
, to get the address of that object. 指针是保存其他内容地址的变量,因此要创建指向对象的指针,您需要使用“地址”运算符
&
来获取该对象的地址。
E* activeE = &e;
Now you'll get the error you claimed to get, which is because e
is const
, but you are trying to create a non-const pointer to it. 现在,您会得到声称的错误,这是因为
e
是const
,但是您正在尝试创建指向它的非const指针。 That is forbidden, because it would let you modify the const
object through the pointer. 禁止这样做,因为它会让您通过指针修改
const
对象。 You need: 你需要:
const E* activeE = &e;
You have const E& e
in Hashing<E,N>::add_
method, but inside of it you assign e
to pointer to E
- actually this should generate different error: 您在
Hashing<E,N>::add_
方法中具有const E& e
,但是在其中将e
分配给指向E
指针-实际上,这会产生不同的错误:
'const std::__1::basic_string to 'std::__1::basic_string*"
^
so the fix, for this is to change: 因此解决方法是更改:
E * activeE = e;
E * tempE = v1[pos];
to 至
const E * activeE = &e;
const E * tempE = &v1[pos];
const E& e ... E * activeE = e;
I bet you missed a few select characters from that error message, such as *
and &
. 我敢打赌,您从该错误消息中错过了一些选择字符,例如
*
和&
。 You're trying to assign an E
value to a pointer. 您正在尝试将
E
值分配给指针。
As you seem to be unclear about the difference between objects, values and addresses, I'm not going to guess whether v1
is an vector<E>
or a vector<E*>
. 您似乎不清楚对象,值和地址之间的区别,所以我不会猜测
v1
是vector<E>
还是vector<E*>
。 And by extension, I'm not going to guess how the line should be fixed. 顺便说一句,我不会猜测应该如何固定该行。 There are at least two possible fixes with entirely different meanings.
至少有两个可能的含义完全不同的修补程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.