Errors when trying to return variables in template c++ -
so im trying contents of clipboard in windows, , can type im using template it, when trying build, visual studio 2013 rc sends errors:
error 1 error c2440: 'return' : cannot convert 'char *' 'double' error 2 error c2440: 'return' : cannot convert 'double' 'char *' error 3 error c2440: 'return' : cannot convert 'int' 'char *' warning 4 warning c4244: 'return' : conversion 'double' 'int', possible loss of data error 5 error c2440: 'return' : cannot convert 'char *' 'int'
heres code:
template<typename tn> tn getclipboardcontents() { handle h_clip; double d_clip; int i_clip; char* str_clip; if (openclipboard(null)) { h_clip = getclipboarddata(cf_text); if (typeid(tn).name() == "double") { d_clip = atof((char*)h_clip); }else if (typeid(tn).name() == "int"){ i_clip = atoi((char*)h_clip); }else{ str_clip = (char*)h_clip; } closeclipboard(); } if (typeid(tn).name() == "double") return d_clip; else if (typeid(tn).name() == "int") return i_clip; else return str_clip; }
thanks in advance.
you've got multiple return paths return different types. you'll need use template overloading, along lines of:
template <typename tn> tn getclipboardcontents(); template <> double getclipboardcontents<double>() { // add double implementation here } template <> int getclipboardcontents<int>() { // add int implementation here }
repeat other types
Comments
Post a Comment