Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No viable conversion from 'const std::__1::basic_string<char> to 'std::__1::basic_string<char> *'

Tags:

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];
    }

}
like image 291
backfloep Avatar asked May 17 '16 08:05

backfloep


2 Answers

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];
like image 168
marcinj Avatar answered Sep 29 '22 05:09

marcinj


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;
like image 22
Jonathan Wakely Avatar answered Sep 29 '22 04:09

Jonathan Wakely