c++ - invalid conversion from const char* to char*

c++ – invalid conversion from const char* to char*

c++ – invalid conversion from const char* to char*

Well, data.str().c_str() yields a char const* but your function Printfunc() wants to have char*s. Based on the name, it doesnt change the arguments but merely prints them and/or uses them to name a file, in which case you should probably fix your declaration to be

void Printfunc(int a, char const* loc, char const* stream)

The alternative might be to turn the char const* into a char* but fixing the declaration is preferable:

Printfunc(num, addr, const_cast<char*>(data.str().c_str()));

string::c.str() returns a string of type const char * as seen here

A quick fix: try casting printfunc(num,addr,(char *)data.str().c_str());

While the above may work, it is undefined behaviour, and unsafe.

Heres a nicer solution using templates:

char * my_argument = const_cast<char*> ( ...c_str() );

c++ – invalid conversion from const char* to char*

First of all this code snippet

char *addr=NULL;
strcpy(addr,retstring().c_str());

is invalid because you did not allocate memory where you are going to copy retstring().c_str().

As for the error message then it is clear enough. The type of expression data.str().c_str() is const char * but the third parameter of the function is declared as char *. You may not assign an object of type const char * to an object of type char *. Either the function should define the third parameter as const char * if it does not change the object pointed by the third parameter or you may not pass argument of type const char *.

Related posts on c++ :

Leave a Reply

Your email address will not be published.