Search code examples
c++pointersstdstring

a pointer bound to a function may only be used to call a function


I've just moved from char arrays to std::string and I've already ran into a problem, I'm probably doing something extremely stupid, feel free to ridicule:

int main()
{
    string * p = new string;
    memset(p, 0, sizeof(string));

    expected_exepath(p);

    cout << p->data;

    delete p;
}

The error is in p->data, which says "a pointer bound to a function may only be used to call a function".. p is std::string, so I don't see why it thinks I'm trying to call a function.


Solution

  • Because data is a function, not a data member. More importantly, half the point of std::string is that it's a value. You shouldn't use new unless you have an extremely good reason- allocate on the stack, or if you must dynamically allocate use a container or smart pointer.

    Also: Do not ever, ever, ever memset UDTs like that. They take care of their own internal state, all the time, and do not mess with it.