c ++错误:没有函数模板的实例

时间:2014-03-04 10:02:09

标签: c++ lua function-templates

我试图用c ++从我的config.lua文件中获取变量。 我从教程中创建了一个Lua-Class来获取这些变量,但我收到了一个错误 当我尝试调用从config.lua

获取变量的函数时

以下是代码段:

LuaScript script("config.lua");
script.get(string("test"));

我收到错误,“没有函数模板的实例与参数列表匹配”,在我称之为“script.get(string(”test));“

模板功能和专业化如下所示:

template<typename T>
T get( const std::string &variableName )
{
    if (!L)
    {
        printError(variableName, "Script not loaded");
        return lua_getdefault<T>();
    }

    T result;
    if (lua_gettostack(variableName))
    {
        result = lua_get<T>(variableName);
    }else{
        result = lua_getdefault<T>();
    }

    clean();
    return result;
}

专业化:

template<>
inline std::string LuaScript::lua_get<std::string>( const std::string &variableName )
{
std::string s = "null";
if (lua_isstring(L, -1))
{
    s = std::string(lua_tostring(L, -1));
}else{
    printError(variableName, "Not a string");
}

return s;
}

仅提供更多信息,我正在使用Visual Studio 2012进行编码和编译。

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

编译器不知道模板化T的{​​{1}},因为它只是一个返回值(即使您要将返回值赋给变量,C ++也不会推断{{1}基于返回值)。所以你必须明确告诉编译器什么是get()。此外,您不需要创建临时字符串,因为只有一种参数类型可能(T),所以试试这个:

T