C ++ - 已弃用从字符串常量到'char *'的转换 [英] C++ - deprecated conversion from string constant to ‘char*’

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

问题描述


可能重复:

从字符串常量到字符串常量的过时转换错误


我试图运行旧的C ++代码今天(这个代码走在2004年:)。但现在我得到了这个错误消息:

  make [1]:输入目录`/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 ../ .. -g -O2 -Wno-deprecated -g -O2 -c -o error.o`test -f'error.C'|| echo'./'`error.C
error.C:在构造函数'error_handler :: error_handler(const char *,char *)':
error.C:49:7:error:'cerr '未在此范围中声明
error.C:58:11:warning:不推荐从字符串常量转换为'char *'[-Wwrite- strings]
error.C:58:11:warning :不推荐从字符串常量转换为'char *'[-Wwrite- strings]
error.C:58:11:warning:不推荐从字符串常量转换为'char *'[-Wwrite- strings]
error.C:58:11:warning:从字符串常量转换为'char *'[-Wwrite- strings]
make [1]:*** [error.o]错误1
make [1]:离开目录`/home/thehost/Plocha/lpic-1.3.1/lpic/src'
make:*** [all-recursive]错误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:无法打开错误文件< 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错误(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 *

/ code> s,但这是非常不安全的。写入由字符串字面量定义的数组会导致未定义行为(导致安全漏洞,程序崩溃等事件的类型),但是一个常规的ol < c> char *
指针将允许你做这种类型的写。由于这个原因,这是强烈建议你使所有 char * ,将指向一个C风格的字符串,而不是 const char * s,以便编译器可以检查以确保您不会尝试写入它们。在这种情况下,你的代码最好写成

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

但是,由于您使用的是C ++,很多只是使用 std :: string

 :string m1,
std :: string m2 =,std :: string m3 =,std :: string m4 =);

这完全避免了这个问题,因为C ++ std :: string 类型在其参数中正确地具有 const char * ,并且创建了字符串的深层副本,因此如果您尝试变异字符串,



希望这有助于!


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

I tried to run old C++ code today (this code gone right in 2004 :). But now I got this error message:

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:

...
#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:

...
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!

解决方案

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 strongly suggested 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

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

However, since you're using C++, a much better idea is just to use 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.

Hope this helps!

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

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