Question

I'am trying to get a variable out of my config.lua file with c++. I've created a Lua-Class from a tutorial to get these variable but I'am getting an error when I try to call the function who gets the variable from config.lua

here are the code snippets:

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

I'am getting the error, "no instance of function template matches the argument list", at the point where I call "script.get(string("test));"

the template function and specialization looks like this:

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;
}

the specialization:

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;
}

Just for some more information, I'am coding and compiling with Visual Studio 2012.

thanks for your help :)

Was it helpful?

Solution

The compiler does not know T of your templated get() since it is only a return value (and even if you were to assign the return value to a variable, C++ does not infer T based on return values). So you must explicitly tell compiler what is T. Also, you do not need to create the temp string, since there is only one parameter type possible (const std::string&), so try this:

script.get<std::string>("test");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top