从字符串常量到char *错误的弃用转换 [英] Deprecated conversion from string constant to char * error

查看:408
本文介绍了从字符串常量到char *错误的弃用转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Possible Duplicate:
C++ deprecated conversion from string constant to ‘char*’

我有以下代码,虽然我没有复制完整的代码,因为它是巨大的。
以下代码在模板类中,我收到如下警告。由于模板中的警告,我无法实例化它并获得从这里实例化错误。

I am having following code, though i didn't copy full code because it is huge. Following code is in template class, and i am getting warning as below. Because of warning in template i am not able to instantiate it and getting "instantiated from here" error.


警告:不推荐从字符串常量转换至'char *''

warning: deprecated conversion from string constant to 'char*''



void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}


char cCompleteMessage[200];
memset(cCompleteMessage, 0x00, sizeof(cCompleteMessage));
char*cMessage = "add reorgenize failed";
ErrorMessageInRaphsodyCode(cCompleteMessage, cMessage, "omcollec.h", __LINE__);

我的问题是什么是最好的方法来摆脱上述警告?

My question is what is best way to get rid of above warning ?

推荐答案

如果函数接受 char const * ,它保证它只读取指针指着。但是,如果它使用一个非常量指针,如 char * ,它可能会写入。

If a function takes a char const *, it guarantees that it only reads whatever data the pointer points to. However, if it takes a non-const pointer, like char *, it might write to it.

写入字符串字面值是不合法的,编译器将发出警告。

As it is not legal to write to a string literal, the compiler will issue a warning.

最好的解决方法是更改​​函数接受 char const * 而不是 char *

The best solution is to change the function to accept char const * rather than char *.

这篇关于从字符串常量到char *错误的弃用转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆