0

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 :)

chrisoo
  • 17
  • 1
  • 6
  • 1
    `LuaScript::lua_get` **is not a specialization** of the function template (`get()`) you show. – πάντα ῥεῖ Mar 04 '14 at 10:06
  • so do I need to write a specialization or is my error somewhere else? because the get() function calls the lua_getdefault – chrisoo Mar 04 '14 at 11:33
  • whoever downvoted, why the downvote? OP didn't say the specialization was for get(); they correctly showed the specialization of lua_get for string since that is the one they thought was being instantiated (but wasn't, as per my answer). – Oliver Mar 04 '14 at 19:44
  • @Schollii I didn't downvote anything here?!? – πάντα ῥεῖ Mar 04 '14 at 19:46
  • @πάνταῥεῖ Sorry, I guess the downvoter didn't leave a comment, unfortunate. I edited my comment. – Oliver Mar 04 '14 at 19:49
  • @Schollii De nada! But I think the question was originally downvoted because it's not asked very clear, and using the term _specialization_ wrongly. I didn't downvote, but AFAIR I also [honestly decided](http://stackoverflow.com/help/behavior) the question wasn't worth upvoting. – πάντα ῥεῖ Mar 04 '14 at 19:53

1 Answers1

0

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");
Oliver
  • 27,510
  • 9
  • 72
  • 103