I'm currently working on an project for a class in which I've to implement cuckoo hashing in C++. The problem is, that C++ and I were never friends and I think we never will be...
The concrete problem is, that I can't wangle to set an pointer on an already existing Object. When I do so, I get the compile error:
No viable conversion from 'const std::__1::basic_string to 'std::__1::basic_string'*
The error occurs for both statements:
E * activeE = e;
E * tempE = v1[pos];
v1 is an array of E Objects.
I think this error is caused by my generally misunderstanding in the basic concept of 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];
}
}
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:
'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];
I get the compile error:
No viable conversion from 'const std::__1::basic_string to '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. That is forbidden, because it would let you modify the const
object through the pointer. You need:
const E* activeE = &e;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With