ven*_*rty 3 c++ coding-style compiler-warnings
可能重复:
C++不推荐将字符串常量转换为'char*'
我有以下代码,虽然我没有复制完整的代码,因为它是巨大的.以下代码在模板类中,我收到如下警告.由于模板中的警告,我无法实例化它并从此处"实例化"错误.
警告:已弃用从字符串常量转换为'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__);
Run Code Online (Sandbox Code Playgroud)
我的问题是什么是摆脱上述警告的最佳方法?
Lin*_*cer 11
如果函数采用a char const *,则保证它只读取指针指向的任何数据.但是,如果它需要一个非const指针char *,它可能会写入它.
由于写入字符串文字是不合法的,编译器将发出警告.
最好的解决方案是将功能更改为接受char const *而不是char *.
char cMessage[] = "add reorganize failed";
Run Code Online (Sandbox Code Playgroud)
这应该摆脱警告.
| 归档时间: |
|
| 查看次数: |
12639 次 |
| 最近记录: |