我正在尝试编写一组函数,将字符串转换为对应的数字值,如"int、float、double"等。但是我的模板函数没有被C ++编译器识别。
当我调用它时,我收到一个消息,指出“没有匹配的函数调用”。
我从这个函数返回T类型的函数,但我得到了这个错误消息,我试图修改这个函数,使它可以通过引用传递的变量来改变值,但错误仍然存在。
我已尝试修改此函数,但它仍然不起作用,请帮忙。
我有几个版本的这个函数进行了覆盖,如下:
static void convertValue(eOperandType type, const std::string &value, FloatOperandType typeDeterminer, T &container)
{
try
{
container = std::stof(value);
}
catch (std::invalid_argument e)
{
throw AVMException("The value passed as number is not valid");
}
catch (std::out_of_range e)
{
char *msg = "The value passed is out of the range of the typed passed";
throw AVMException(msg);
}
catch (exception& e)
{
throw AVMException("An unexpected error occured when converting value types");
}
}
template <typename T>
static void convertValue(eOperandType type, const std::string &value, IntOperandType typeDeterminer, T &container)
{
int val = 0;
try
{
val = std::stoi(value);
switch(type)
{
case eOperandType::Int8:
if(val < -128 || val > 127)
throw AVMWarnException("Overflow for type Int8");
case eOperandType::Int16:
if(val < -32768 || val > 32,767)
throw AVMWarnException("Overflow for type Int16");
case eOperandType::Int32:
if(val < -2147483648 || val > 2147483647)
throw AVMWarnException("Overflow for type Int32");
}
container = std::stoi(value);
}
catch (std::invalid_argument e)
{
throw AVMException("The value passed as number is not valid");
}
catch (std::out_of_range e)
{
char *msg = "The value passed is out of the range of the typed passed";
throw AVMException(msg);
}
catch (exception& e)
{
throw AVMException("An unexpected error occured when converting value types");
}
}
这是我调用该函数的方式:
int v = 0;
Converter::convertValue<int>("20", eOperandType::Int8, IntOperandType::_Int8, &v);