C++ 从“const char*”到“char*”的无效转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20984220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalid conversion from 'const char*' to 'char*'
提问by user2333234
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.
retstring()
是一个返回字符串的函数。
//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
从“const char*”到“char*”的无效转换。
在函数的参数 3 上初始化“void Printfunc(int, char*, char*)”的参数 3
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.
如果我需要更改任何初始化,请告诉我。
回答by Dietmar Kühl
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
好吧,data.str().c_str()
产生 achar const*
但你的函数Printfunc()
想要有char*
s。根据名称,它不会更改参数,而只是打印它们和/或使用它们来命名文件,在这种情况下,您可能应该将声明修改为
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:
另一种方法可能是将 thechar const*
变成 achar*
但更可取的是修复声明:
Printfunc(num, addr, const_cast<char*>(data.str().c_str()));
回答by Louis93
string::c.str()
returns a string of type const char *
as seen here
string::c.str()
返回一个类型的字符串,const char *
如这里所示
A quick fix: try casting printfunc(num,addr,(char *)data.str().c_str())
;
快速修复:尝试铸造printfunc(num,addr,(char *)data.str().c_str())
;
While the above may work, it is undefined behaviour, and unsafe.
虽然上述可能有效,但它是未定义的行为,并且不安全。
Here's a nicer solution using templates:
这是使用模板的更好的解决方案:
char * my_argument = const_cast<char*> ( ...c_str() );
回答by Vlad from Moscow
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().
无效,因为您没有在要复制 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 *.
至于错误信息,那就很清楚了。表达式 data.str().c_str() 的类型是 const char * 但函数的第三个参数被声明为 char *。您不能将 const char * 类型的对象分配给 char * 类型的对象。如果函数不更改第三个参数指向的对象,则该函数应将第三个参数定义为 const char *,或者您可能未传递类型为 const char * 的参数。