Search code examples
c++operator-keyworddereferencedouble-pointer

Cannot dereference double pointer, " no match for operator* "


I'm trying to search through an array of pointers to objects of class Shape. I have written the following code. However, I'm getting this error: "no match for operator*", and I don't know where to go from here. Any help is appreciated.

Shape** shapesArray;

bool doesNameExist(string name) {
    for (int i = 0; i < shapeCount; i++)
    {
        if(*(shapesArray[i])->getName() == name)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}

Solution

    • shapesArray is a Shape**

    • shapesArray[i] is Shape*

    • (shapesArray[i])->getName() is dereferencing shapesArray[i] and calls its member getName

    So far nothing wrong. I guess this is what you actually want to get, but you add another *:

    • *(shapesArray[i])->getName() tries to dereference what was returned from getName (a std::string perhaps?)

    PS: You return from the loop in the first iteration in either case. If you want to search in the array you need to loop until you find it (then return true) or loop till the end (then return false after the loop, because it wasn't found).