简体   繁体   English

从“const char*”到“char*”的无效转换

[英]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. retstring()是一个返回字符串的函数。

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

I get the error我得到错误

invalid conversion from 'const char*' to 'char*'.从“const char*”到“char*”的无效转换。

initializing argument 3 of 'void Printfunc(int, char*, char*)'on argument 3 of the function在函数的参数 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.如果我需要更改任何初始化,请告诉我。

Well, data.str().c_str() yields a char const* but your function Printfunc() wants to have char* s. 好吧, data.str().c_str()产生一个char const*但是您的函数Printfunc()想要有char* 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: 替代方法可能是将char const*转换为char*但最好固定声明:

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

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() );

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 *. 表达式data.str()。c_str()的类型为const char *,但该函数的第三个参数声明为char *。 You may not assign an object of type const char * to an object of type char *. 您不能将const char *类型的对象分配给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 *. 如果该函数未更改第三个参数所指向的对象,或者您可能未传递const char *类型的参数,则该函数应将第三个参数定义为const char *。

c_str() will give you a const char * c_str() 会给你一个 const char *

For converting const char * to char * you can use strdup like this.要将 const char * 转换为 char *,您可以像这样使用 strdup。

char * success = strdup(data.str().c_str());

also it will make a new malloc so it creates a duplicate.它还会创建一个新的 malloc,因此它会创建一个副本。

so you need to do this所以你需要这样做

char * success = strdup(data.str().c_str());
printfunc(num, addr, success);
free(success);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM