The*_*ect 2 c++ templates linker-errors c++11
我发现了一些C++ 11的功能并且有问题.我有一个成员函数'call'
class cscript
{
public:
template <typename ret_t, typename... params>
bool call(ret_t &ret, const char * name, params... parameters);
....
Run Code Online (Sandbox Code Playgroud)
执行:
template <typename ret_t, typename... params>
bool cscript::call(ret_t &ret, const char * name, params... parameters)
{
ret_t (*func)(params...);
func = (decltype(func)) tcc_get_symbol(tcc, name);
if (!func)
return true;
ret = func(parameters...);
return false;
}
Run Code Online (Sandbox Code Playgroud)
链接时显示以下错误:
obj\Release\main.o:main.cpp:(.text.startup+0xcc)||undefined reference to `bool cscript::call<int, int, int>(int&, char const*, int, int)'|
Run Code Online (Sandbox Code Playgroud)
电话示例:
script.call(ret, "sum", 2, 3);
Run Code Online (Sandbox Code Playgroud)
有关如何使这项工作的任何建议?
从它的外观来看,模板定义在使用时是不可见的.也就是说,编译器在看到它的实现时不知道它需要传递什么模板参数,cscript::call()并且它不知道在使用它时实现的样子.您有两个基本选项:
cscript::call()放入标题中,以便在使用时始终可以看到它.后者看起来像下面的,在实现文件中的定义之后的某个地方:
template bool cscript::call<int, int, int>(int&, char const*, int, int);
Run Code Online (Sandbox Code Playgroud)
从它的外观你想要使用这个模板的几个不同版本,你可能不想重复的事情.因此,您可能希望将定义放入标题中.