C++ - 不推荐使用从字符串常量到“char*”的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7896645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ - deprecated conversion from string constant to ‘char*’
提问by user1013619
Possible Duplicate:
Deprecated conversion from string constant to char * error
可能的重复:
不推荐将字符串常量转换为 char * 错误
I tried to run old C++ code today (this code gone right in 2004 :). But now I got this error message:
我今天尝试运行旧的 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 constant to ‘char*' [-Wwrite- strings]
error.C:58:11: warning: deprecated conversion from string constant to ‘char*' [-Wwrite- strings]
make[1]: *** [error.o] Error 1
make[1]: Leaving directory `/home/thehost/Plocha/lpic-1.3.1/lpic/src'
make: *** [all-recursive] Error 1
Source of "error.C" file:
“error.C”文件的来源:
...
#include <error.h>
int error_handler::error_number = 0;
int error_handler::message_number = 0;
int error_handler::debug_number = 0;
int error_handler::Q_debug = 1;
int error_handler::object_number = 0;
int error_handler::tab = 33;
error_handler::error_handler(const char *name, char *error_file_name)
{
errname = new char [filename_size];
strcpy(errname,error_file_name);
errfile.open(errname,ios::app);
if (!errfile)
{
cerr << "error_handler: cannot open error file " << errname << endl;
exit(1);
}
errfile.close();
my_name = name;
object_number++;
debug("");
}
void error_handler::error(char* s1, char* s2, char *s3, char *s4)
{
error_number++ ;
errfile.open(errname,ios::app);
errfile.setf(ios::left);
errfile << "FAILURE: " << setw(tab) << my_name << " " << s1 << ' ' << s2
<< s3 << s4 << endl;
errfile.close();
exit(1);
}
...
And source of "error.h" file:
和“error.h”文件的来源:
...
using namespace std;
class error_handler {
static int error_number;
static int message_number;
static int Q_debug;
static int debug_number;
static int object_number;
const char *my_name;
char *errname;
ofstream errfile;
static int tab;
public:
error_handler(const char *, char *error_file_name);
void error(char* s1, char* s2="",
char* s3="", char* s4="");
void error(char* s1, double d2,
char* s3="", char* s4="");
void message(char* m1,
char* m2="", char* m3="", char* m4="");
void message(char* m1, double m2,
char* m3="", char* m4="");
void message(char* m1, double m2, char* m3, double m4);
void message(char* m1, double m2, char* m3, double m4,
char* m5, double m6, char* m7, double m8);
void message(char* m1, double m2, double m3, double m4, double m5 );
void message(char* m1, double m2, double m3, double m4 );
void message(char* m1, double m2, char* m3, double m4, char* m5, double m6);
void message(char *s1, double d2, double d3);
void message(char *s1, char *s2, double d3);
void debug(char* m1,
char* m2="", char* m3="", char* m4="");
void debug(char* m1, double m2,
char* m3="", char* m4="");
void debug(char* m1 , double m2, char* m3, double m4);
void debug(char* m1 , double m2, char* m3, double m4, char* m5, double m6);
};
#endif
Have you any idea how I could fix it? If yes, please write it down clearly (I am newbie...). Thank you!
你知道我该如何修复它吗?如果是,请写清楚(我是新手...)。谢谢!
回答by templatetypedef
I think that your warnings are coming from this code:
我认为您的警告来自此代码:
void message(char* m1,
char* m2="", char* m3="", char* m4="");
The issue is that string literals in C++ can be treated as char*
s, but it's very unsafe to do so. Writing to an array defined by a string literal results in Undefined Behavior (the sort of thing that causes security holes, program crashes, etc.), but a regular ol' char*
pointer would allow you to do this sort of write. For this reason, it is stronglysuggested that you make all char*
s that would point to a C-style string instead be const char*
s so that the compiler can check to make sure that you aren't try to write to them. In this case, your code would be better written as
问题是 C++ 中的字符串文字可以被视为char*
s,但这样做非常不安全。写入由字符串字面量定义的数组会导致未定义行为(导致安全漏洞、程序崩溃等的事情),但常规 ol'char*
指针将允许您执行此类写入。出于这个原因,强烈建议您将所有char*
指向 C 样式字符串的const char*
s改为s,以便编译器可以检查以确保您不会尝试写入它们。在这种情况下,你的代码最好写成
void message(char* m1,
const char* m2="", const char* m3="", const char* m4="");
However, since you're using C++, a muchbetter idea is just to use std::string
:
但是,由于您使用C ++中,很多更好的主意只是为了使用std::string
:
void message(std::string m1,
std::string m2="", std::string m3="", std::string m4="");
This completely avoids the issue, because the C++ std::string
type correctly has const char*
s in its arguments and makes a deep-copy of the string, so if you try to mutate the string it's guaranteed that you're not going to be trashing the original array of characters.
这完全避免了这个问题,因为 C++std::string
类型const char*
在其参数中正确地包含s 并制作了字符串的深层副本,因此如果您尝试改变字符串,则可以保证您不会破坏原始字符数组.
Hope this helps!
希望这可以帮助!
回答by asveikau
You have a few options:
您有几个选择:
Fix your code so that string literals (eg.
"foo"
) never get implicitly converted tochar*
. They should beconst char*
.Change your compiler command line to include
-Wno-write-strings
. This is what the-Wwrite-strings
part of the error message is hinting at.
修复您的代码,以便字符串文字(例如
"foo"
)永远不会被隐式转换为char*
. 他们应该是const char*
。将编译器命令行更改为包含
-Wno-write-strings
. 这就是-Wwrite-strings
错误消息的部分所暗示的。
I would prefer the first option.
我更喜欢第一个选项。
回答by baderman
There's an compilation error, which states that cerr is not defined. Other answers tell You how to fix problems higlighted by warning messages. To compile you need to include iostream and use the namespace std (or add namespace before stream name and endl).
有一个编译错误,表明未定义 cerr。其他答案告诉您如何解决警告消息突出显示的问题。要编译,您需要包含 iostream 并使用命名空间 std(或在流名称和 endl 之前添加命名空间)。
Here's some example code:
下面是一些示例代码:
#include <iostream>
using namespace std;
int main( )
{
cerr << "test" << endl;
}
回答by mday299
Try
尝试
#include <iostream>
in the header.
在标题中。