【发布时间】:2013-11-21 20:48:22
【问题描述】:
我正在尝试将字符串转换为数字。为此,我找到了以下方法:
#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;
}
问题是我收到以下错误:
错误:没有匹配的函数调用'stringToNumber(std::string&)'
谁能告诉我为什么会出现这样的错误以及如何解决?
提前谢谢你。
【问题讨论】:
-
这个错误应该更多,比如
T不能被推断出来。 -
您可能还需要在顶部
#include <sstream>才能将 std::stringstream 纳入您的范围。 -
是的,我一修好就意识到 :)
标签: c++ templates type-conversion stdstring