gpt4 book ai didi

C++ - 不推荐从字符串常量到 ‘char*’ 的转换

转载 作者:IT老高 更新时间:2023-10-28 21:39:15 25 4
gpt4 key购买 nike

Possible Duplicate:
Deprecated conversion from string constant to char * error

我今天尝试运行旧的 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

“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);
}
...

以及“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

您知道我该如何解决吗?如果是,请写清楚(我是新手……)。谢谢!

最佳答案

我认为您的警告来自此代码:

void message(char* m1,
char* m2="", char* m3="", char* m4="");

问题是 C++ 中的字符串文字可以被视为 char*s,但这样做非常不安全。写入由字符串字面量定义的数组会导致未定义行为(导致安全漏洞、程序崩溃等的事情),但常规的 ol' char* 指针将允许您这样做这种写法。因此,强烈建议您将所有指向 C 风格字符串的 char* 改为 const char* s 以便编译器可以检查以确保您没有尝试写入它们。在这种情况下,你的代码最好写成

void message(char* m1,
const char* m2="", const char* m3="", const char* m4="");

但是,由于您使用的是 C++,非常 更好的主意就是使用 std::string:

void message(std::string m1,
std::string m2="", std::string m3="", std::string m4="");

这完全避免了这个问题,因为 C++ std::string 类型正确地在其参数中包含 const char* 并制作了字符串的深拷贝,因此,如果您尝试改变字符串,则可以保证您不会破坏原始字符数组。

希望这会有所帮助!

关于C++ - 不推荐从字符串常量到 ‘char*’ 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7896645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com