模板函数未被识别。

3

我正在尝试编写一组函数,将字符串转换为对应的数字值,如"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);

标准库中已经有实现此功能的函数(见atoiatofstrtod)。 - iambrj
从原型中,你的调用应该是 Converter::convertValue<int>(eOperandType::Int8,"20", IntOperandType::_Int8, &v); 而且你忘记在第一个函数中加上 template <typename T>。 - PilouPili
2个回答

2

如果我猜测eOperandType是一个枚举类型,那么你不能将"20"转换为它是有道理的。此外,你传递了一个int*,而期望的是一个int&。请注意你函数的签名:

template <typename T>
void convertValue(eOperandType, const std::string&, IntOperandType, T&)

并将其与调用函数的方式进行比较:

convertValue<int>("20", eOperandType::Int8, IntOperandType::_Int8, &v);

在这里,您的参数具有以下类型:

const char[3], eOperandType, IntOperandType, int*

注意签名不匹配了吗?你可能想这样调用它:

int v = 0;
Converter::convertValue<int>(eOperandType::Int8, "20", IntOperandType::_Int8, v);

其中"20"可以隐式转换为const std::string&,而v是一个类型为int的左值,可以隐式绑定到int&,即int的左值引用。


0

这可能是因为在调用函数时,e 操作数类型之前的字符串不匹配参数列表。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接