由于使用无符号整数,可变参数模板 C++ 上未解决的外部符号错误

Yaf*_*imK 1 c++ templates variadic-templates c++11

我已经用可变参数模板声明了一个简单的函数。

template<typename ...Args>
void Log(const LogLevel level, const char * format, Args ...args);
Run Code Online (Sandbox Code Playgroud)

在以下列方式调用它时 -

Log(LogLevel::debug,
        R"(starting x, %d pending call for "%s" with param "%s")",
        id, first.c_str(),
       second.c_str())
Run Code Online (Sandbox Code Playgroud)

其中变量类型是:id( unsigned int), first( std::string) , second( std::string)

我收到以下错误:

Error   LNK2001 unresolved external symbol "public: void __cdecl Log<unsigned int,char const *,char const *>(enum LogLevel,char const *,unsigned int,char const *,char const *)" 
Run Code Online (Sandbox Code Playgroud)

当我unsigned int从函数调用中删除参数时 - 错误消失了。AFAIK 可变参数模板确实支持不同的类型......那我错过了什么?

max*_*x66 5

这是一个链接器错误,所以(我想)您已经在头文件中声明了模板函数并在 c++(不是头)文件中定义了它。

如果您使用unsigned int在不同的 c++ 文件中接收 的模板函数,编译器不知道要实现哪个版本的函数。

简单的解决方案:在头文件中声明和定义模板函数/类/结构。

如果我错了......请准备一个最小的例子来复制错误。