Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

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

Have a code as shown below. I have problem passing the arguments.

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

retstring() is a function that returns a string.

//more code
printfunc(num,addr,data.str().c_str());

I get the error

invalid conversion from 'const char*' to 'char*'.

initializing argument 3 of 'void Printfunc(int, char*, char*)'on argument 3 of the function

on the above line. The function is called as shown below

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

please let me know if I need to change any initialization.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, data.str().c_str() yields a char const* but your function Printfunc() wants to have char*s. Based on the name, it doesn't 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()));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...