2013-07-31 50 views
2

我不明白爲什麼我的編譯器給我一個關於從字符串到char的棄用轉換的警告。C++ - 棄用從字符串常量轉換爲字符

這是在抱怨警告:

只是有點我在做什麼..我試圖理解和實踐例外背景的......我不知道,如果它能夠更好只是與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; 
    } 
}; 

=====================異常類========================== =======

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();} 
}; 
+1

哪條線是錯誤的?什麼是實際的錯誤信息? –

+0

如何定義'Exception'? –

+2

'getters'返回3個值? –

回答

14

我想Exception的構造函數簽名

Exception(int, char*) 

你傳遞一個字符串作爲參數,其實際類型爲const char*,但隱式轉換char*是合法的pre-C++ 11(但不贊成,所以你會得到警告)。

修改簽名

Exception(int, const char*) 

,或者更好的,

Exception(int, const std::string&) 

總結:

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

字符串文字的類型是'const char [N]' –

相關問題