C++ error cannot specify explicit initializer for arrays for char g[] -
i have declared following struct
:
const struct database_already_exists { const int code = 2001; const char str[] = "database exists."; };
but when pass function:
database_already_exists db_err; error_output(db_err.str, db_err.code);
it throws following error(visual studio 2013):
cannot specify explicit initializer arrays
here declaration error_output:
template<class d> char* error_output(d err_str, int err_code = 0) { return "(" + err_code + ") " + err_str; }
how should change definition str
member of struct
eliminate such error?
i think can modify code below; return "(" + err_code + ") " + err_str;
doesnt make sense, can't apply +
operator on 2 char *
's .
#include <string> #include <iostream> #include <cstring> #include <sstream> struct database_already_exists { database_already_exists(const char* s) : str(s) {} static const int code = 2001; const char *str; }; template<class d> const char* error_output(d err_str, int err_code = 0) { std::istringstream is; is>>err_code; std::string str; str += "("; str += is.str(); str += ") "; str += err_str; return str.c_str(); } int main(void) { database_already_exists db_err("database exists."); const char *res = error_output(db_err.str, db_err.code); std::cout<<res<<std::endl; return 0; }
Comments
Post a Comment