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
  • 74,985
  • 8
  • 76
  • 165
YafimK
  • 195
  • 4
  • 15
  • "I've declared a simple function with variadic template." And where and how did you define it? – O'Neil Dec 19 '17 at 16:23
  • 1
    If your template function *definition* is *not* in a header file, see [this](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). Otherwise, post a [mcve]. – n. m. could be an AI Dec 19 '17 at 16:27
  • Missed this! Thanks for the link to the relevant post. – YafimK Dec 20 '17 at 06: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
  • 65,235
  • 10
  • 71
  • 111