2014-03-04 52 views
0

我試圖從我的config.lua文件中用C++獲取一個變量。 我從一個教程中創建一個Lua-Class,以獲得這些變量,但我'得到一個錯誤 當我嘗試打電話給誰從config.lua得到可變C++錯誤:沒有函數模板的實例

這裏的代碼片段的功能:

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

我'得到的錯誤,「沒有函數模板實例參數列表匹配」,在那裏我稱之爲點「script.get(字符串(」測試));」

模板功能和專業化看起來像這樣:

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

'LuaScript :: lua_get' **不是專業化的**函數模板('get()')顯示。 –

+0

所以我需要寫一個專業化或是我的錯誤在別的地方?因爲get()函數調用lua_getdefault – chrisoo

+0

誰downvoted,爲什麼downvote? OP沒有說專業化是爲get();他們正確地顯示了字符串lua_get 的專業化,因爲那是他們認爲正在實例化(但不是,根據我的答案)。 – Schollii

回答

0

編譯器不編譯知道T您的模板get(),因爲它只是一個返回值(即使您將返回值分配給一個變量,C++不會根據返回值推斷T)。所以你必須明確地告訴編譯器什麼是T。此外,您不需要創建臨時字符串,因爲只有一個參數類型的可能(const std::string&),那麼試試這個:

script.get<std::string>("test");