简体   繁体   English

从字符串文字转换为字符*

[英]Conversion from string literal to char *

I am trying to make ac function work in some publicly available linear algebra code. 我试图在一些公开的线性代数代码中使交流功能起作用。

the publicly available prototype is… 公开的原型是......

int ilaenv_(int *, char *, char *, int *, int *,int *, int *);

The publicly available code has the function call… 公开的代码有函数调用...

nb = ilaenv_(&c__1, "DGEQRF", " ", m, n, &c_n1, &c_n1); 

where m, n, c_1, and c_n1 are integers, 其中m,n,c_1和c_n1是整数,

The error message is. 错误消息是。

C++ 11 does not allow conversation from string literal to char *.

I did not create the code, but downloaded it from the LAPACK site. 我没有创建代码,但是从LAPACK站点下载了代码。 I hesitate to make too many changes to publicly available code that supposedly works, for fear of introducing errors. 我不愿意对可能有效的公开代码进行太多更改,以免引入错误。 However, this error is showing up on a number of functions in the program that I am working on. 但是,此错误出现在我正在处理的程序中的许多函数中。

How can I resolve this? 我该如何解决这个问题?

To be safe, you could create char arrays initialised to the same values, for example: 为安全起见,您可以创建初始化为相同值的char数组,例如:

char dgeqrf[] = "DGEQRF";
char space[] = " ";

Or you could check the source code of the function; 或者你可以查看函数的源代码; if it doesn't actually modify the contents of those arrays you could change the arguments to const char * . 如果它实际上没有修改那些数组的内容,你可以将参数更改为const char *

Your function takes "char *", not "const char *". 你的函数采用“char *”,而不是“const char *”。 String literals can be assigned only to "const char *". 字符串文字只能分配给“const char *”。

Sorry for being so late, I just bumped into this problem. 很抱歉这么晚,我刚刚碰到这个问题。 You can try this, 你可以试试这个,

string dgeqrf = "DGEQRF";

One problem with using "const char *" is code like this, 使用“const char *”的一个问题是这样的代码,

string formatString;
switch (format)
{
    case FORMAT_RGB:  formatString = "RGB"; break;
    case FORMAT_RGBA: formatString = "RGBA"; break;
    case FORMAT_TP10: formatString = "TP10"; break;
    default:          formatString = "Unsupported"; break;
}

If you declare formatString as a "const char *", this won't work. 如果将formatString声明为“const char *”,则不起作用。

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

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