1

I've declared a simple function with variadic template.

template<typename ...Args>
void Log(const LogLevel level, const char * format, Args ...args);

Upon calling it in the following manner -

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

where the variable types are : id (unsigned int), first (std::string) , second (std::string)

I'm getting the following error:

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 *)" 

When I remove the unsigned int argument from the function call - the error disappears. AFAIK the variadic template does support different types... so what am I missing?

Pete Becker
  • 72,338
  • 6
  • 72
  • 157
YafimK
  • 185
  • 3
  • 14

1 Answers1

5

It's a linker error, so (I suppose) you have declared the template function in a header file and defined it in a c++ (not header) file.

If you use the template function that receive the unsigned int in a different c++ file, the compiler doesn't know which versions of the function to implement.

Simple solution: declare and define the template functions/classes/structs in the headers.

If I'm wrong... please prepare a minimal example to replicate the error.

max66
  • 63,246
  • 10
  • 70
  • 107