我试图将字符串转换为数字。对于这一点,我发现下面的方法:编译模板错误 - 没有匹配的调用函数
#include <iostream>
#include <string>
template <typename T>
T stringToNumber(const std::string &s)
{
std::stringstream ss(s);
T result;
return ss >> result ? result : 0;
}
int main()
{
std::string a = "254";
int b = stringToNumber(a);
std::cout << b*2 << std::endl;
}
的问题是,我收到以下错误:
error: no matching function for call to ‘stringToNumber(std::string&)’
五月谁能告诉我为什么我收到这样的错误,以及如何解决它?
预先感谢您。
应该有更多的错误,喜欢的事实,'T'无法被推断。 – chris
您可能需要'#include'将std :: stringstream放到您的作用域中。 –
是的,我意识到一旦我修好了:) – XNor