With C++ I am trying to implement a toString() function for my class:
void ClassName::toString(string& returnString)
{
sprintf(returnString.c_str(), "Position: (%f, %f, %f)\n", position.x, position.y, position.y);
}
However I keep getting this error: argument of type const char* is incompatible with parameter of type char*
How do I fix this and make the argument no longer const?
Use a different approach, use a
stringstream
instead or an auxiliary buffer.Don't. Hell will break lose if you change
c_str
to a non-const type. Fellow programmers will weep. Angels will lose their wings. Kittens will die. Oil prices will rise. Stock-market crashes. Zombie apocalipse.It's
const
for a reason - so you don't modify it. Doing so would result in undefined behaviour.