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

我正在尝试使用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进行编码和编译。

谢谢你的帮助 :)

点赞
用户869951
用户869951

编译器不知道您模板化 get() 中的 T,因为它只是一个返回值(即使您将返回值分配给变量,C++ 也不会根据返回值推断出 T)。因此,您必须明确告诉编译器 T 是什么。此外,您不需要创建临时字符串,因为只有一种可能的参数类型( const std::string&),因此请尝试使用以下代码:

script.get<std::string>("test");
2014-03-04 16:53:42