c++ - Trouble with template and stringstream -


i want create function convert string number using stringstream. if suppose number int:

int stringtonumber(string str) {     stringstream ss;     ss << str;     int num;     ss >> num;     return num; } cout << stringtonumber("182") + 100 << endl; //282 

this code works correctly. when try use template error. below code:

template <typename number> number stringtonumber(string str) {     stringstream ss;     ss << str;     number num;     ss >> num;     return num; } 

the errors:

main.cpp: in function ‘int main()’: main.cpp:17:33: error: no matching function call ‘stringtonumber(const char [4])’      cout << stringtonumber("125") + 280 << endl;                                  ^ main.cpp:17:33: note: candidate is: main.cpp:6:8: note: template<class number> number stringtonumber(std::string)  number stringtonumber(string str)         ^ main.cpp:6:8: note:   template argument deduction/substitution failed: main.cpp:17:33: note:   couldn't deduce template parameter ‘number’      cout << stringtonumber("125") + 280 << endl; 

your template argument cannot deduced way. have provide template argument explicitly:

std::cout << stringtonumber<int>("125"); 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -