简体   繁体   English

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

[英]C++ - Deprecated conversion from string constant to char

I don't understand why my compiler is giving me a warning about a deprecated conversion from string to char. 我不明白为什么我的编译器给我一个关于从字符串到char的弃用转换的警告。

this is where is complaining about the warning: 这是抱怨警告的地方:

Just a bit of background of what I'm doing.. I'm trying to understand and practice Exceptions... I'm not sure if its better to just work with char[1000] for the First name and so on.. I will really appreciate if someone help to understand the warning and help me to find a solution.. Thanks.. 只是我正在做的一些背景...我正在尝试理解和练习例外...我不确定是否更好地使用char [1000]作为名字等等。如果有人帮助理解警告并帮助我找到解决方案,我将非常感激..谢谢..

================================================================================= ================================================== ===============================

class TeLoEnYuco
{
string FN, LN, R;
    double Income;

public:
    const char *getters(){return FN.data(), LN.data(), R.data();}
    virtual char *getFacilityAccess()=0;
    TeLoEnYuco(char *fn, char *ln, char r, double inc)
    {
        if(fn==0) throw Exception(1, "First Name is Null"); //Warning #1
        if(ln==0) throw Exception(2, "Last Name is Null");  //Warning #2
        if(r==0) throw Exception(3, "Rank is Null");        //Warning #3
        if(inc<=0) throw Exception(4, "Income is Null");    //Warning #4

        FN=fn;
        LN=ln;
        R=r;
        Income=inc;
    }
};

=====================Exception class================================= =====================异常类=========================== ======

class Exception
{
    int Code;
    string Mess;

public:
    Exception(int cd, char *mess)
    {
        Code=cd;
        Mess=mess;
    }
    int getCode(){return Code;}
    const char *getMess(){return Mess.data();}
};

I assume Exception 's constructor signature is 我假设Exception的构造函数签名是

Exception(int, char*)

You pass a string literal as a parameter, whose actual type is const char* , but the implicit conversion to char* is legal pre-C++11 (but deprecated, so you get the warning). 您将字符串文字作为参数传递,其实际类型为const char* ,但隐式转换为char*是合法的C ++之前的11(但不推荐使用,因此您会收到警告)。

Modify the signature to 将签名修改为

Exception(int, const char*)

or, better yet, 或者,更好的是,

Exception(int, const std::string&)

To summarize: 总结一下:

char* x       = "stringLiteral";  //legal pre-C++11, deprecated
const char* y = "stringLiteral";  // good
std::string z  ("stringLiteral"); // even better

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免在C ++中不推荐将字符串常量转换为&#39;char *&#39; - How to avoid deprecated conversion from string constant to 'char*' in C++ C ++不推荐将字符串常量转换为'char *' - C++ deprecated conversion from string constant to 'char*' C ++警告:不建议将字符串常量转换为&#39;char *&#39;[-Wwrite-strings] - C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] 如何在C ++中修复从字符串常量到&#39;char *&#39;的警告弃用转换? - How can I fix warning deprecated conversion from string constant to 'char*' in C++? C ++-不建议在第三方标头中将字符串常量转换为&#39;char *&#39; - C++ - deprecated conversion from string constant to ‘char*’ in 3rd-party header 从字符串常量到&#39;char *&#39;的错误不建议使用的转换 - ERROR deprecated conversion from string constant to 'char*' C ++:不推荐使用从字符串常量转换为'LPSTR {aka char *}'[-Wwrite-strings] - 警告。怎么避免? - C++: deprecated conversion from string constant to 'LPSTR {aka char*}' [-Wwrite-strings] - warning. How to avoid? 无法通过C ++警告:(并且崩溃之后)不推荐将字符串常量转换为&#39;char *&#39;[-Wwrite-strings] - Can't get pass C++ warning: (& crash after) deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] 从字符串常量到&#39;char *&#39;的不推荐使用转换:不能使用常量 - Deprecated conversion from string constant to 'char*': Can't use the constant 从字符串常量到&#39;char *&#39;[-Wwrite-strings]的弃用转换 - deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM