所以我正在开发一个非常大的代码库,最近升级到gcc 4.3,它现在触发了这个警告:
警告:不推荐将字符串常量转换为'char*'
显然,解决这个问题的正确方法是找到每个声明
char *s = "constant string";
Run Code Online (Sandbox Code Playgroud)
或函数调用如:
void foo(char *s);
foo("constant string");
Run Code Online (Sandbox Code Playgroud)
并使他们成为const char指针.但是,这意味着触及564个文件,最小,这不是我希望在此时执行的任务.现在的问题是我正在运行-werror,所以我需要一些方法来扼杀这些警告.我怎样才能做到这一点?
我有一个班级 private char str[256];
为此,我有一个显式的构造函数:
explicit myClass(const char *func)
{
strcpy(str,func);
}
Run Code Online (Sandbox Code Playgroud)
我称之为:
myClass obj("example");
Run Code Online (Sandbox Code Playgroud)
当我编译这个时,我得到以下警告:
不推荐将字符串常量转换为'char*'
为什么会这样?
可能重复:
从字符串常量到char*错误的过时转换
我今天试图运行旧的C++代码(这个代码在2004年正确:).但现在我收到此错误消息:
make[1]: Entering directory `/home/thehost/Plocha/lpic-1.3.1/lpic/src'
source='error.C' object='error.o' libtool=no \
depfile='.deps/error.Po' tmpdepfile='.deps/error.TPo' \
depmode=gcc3 /bin/bash ../../config/depcomp \
g++ -DHAVE_CONFIG_H -I. -I. -I../.. -g -O2 -Wno-deprecated -g -O2 -c -o error.o `test -f 'error.C' || echo './'`error.C
error.C: In constructor ‘error_handler::error_handler(const char*, char*)’:
error.C:49:7: error: ‘cerr’ was not declared in this scope
error.C:58:11: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite- strings]
error.C:58:11: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite- strings]
error.C:58:11: warning: deprecated conversion from string …Run Code Online (Sandbox Code Playgroud) C语言中这两种形式的字符串变量有什么区别?
char *string1;
char string2[];
Run Code Online (Sandbox Code Playgroud)
还有其他办法吗?
非常感谢你.