C++ - Deprecated conversion from string constant to char -
i don't understand why compiler giving me warning deprecated conversion string char.
this complaining warning:
just bit of background of i'm doing.. i'm trying understand , practice exceptions... i'm not sure if better work char[1000] first name , on.. appreciate if understand warning , me find solution.. thanks..
=================================================================================
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 null"); //warning #1 if(ln==0) throw exception(2, "last name null"); //warning #2 if(r==0) throw exception(3, "rank null"); //warning #3 if(inc<=0) throw exception(4, "income 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
exception(int, char*)
you pass string literal parameter, actual type const char*
, implicit conversion char*
legal pre-c++11 (but deprecated, warning).
modify 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"; // std::string z ("stringliteral"); // better
Comments
Post a Comment