Class
The following code works great (example taken from cppreference.com):
class SomeClass
{
public:
void tprintf(const char* format) // base function
{
std::cout << format;
}
template<typename T, typename... Targs>
void tprintf(const char* format, T value, Targs... Fargs) // recursive variadic function
{
for (; *format != '\0'; format++) {
if (*format == '%') {
std::cout << value;
tprintf(format + 1, Fargs...); // recursive call
return;
}
std::cout << *format;
}
}
};
void main()
{
SomeClass someClass;
someClass.tprintf("% world% %\n", "Hello", '!', 123);
}
Lib
When I try to replicate this by making a lib of the SomeClass class i get an unresolved external symbol error:
SomeClass.h
class SomeClass
{
public:
void tprintf(const char* format);
template<typename T, typename... Targs>
void tprintf(const char* format, T value, Targs... Fargs);
}
SomeClass.cpp
void SomeClass::tprintf(const char* format) // base function
{
std::cout << format;
}
template<typename T, typename... Targs>
void SomeClass::tprintf(const char* format, T value, Targs... Fargs) // recursive variadic function
{
for (; *format != '\0'; format++) {
if (*format == '%') {
std::cout << value;
tprintf(format + 1, Fargs...); // recursive call
return;
}
std::cout << *format;
}
}
main.cpp
#include <ibppWrapper.h>
void main()
{
SomeClass someClass;
someClass.tprintf("% world% %\n", "Hello", '!', 123);
}
The unresolved external symbol error:
error LNK2019: unresolved external symbol "public: void __thiscall SomeClass::tprintf<char const *,char,int>(char const *,char const *,char,int)" (??$tprintf@PBDDH@SomeClass@@QAEXPBD0DH@Z) referenced in function _main
Conclusion
It seems like the linker is searching for a function in my SomeClass lib with the exact parameters:
SomeClass::tprintf<char const *,char,int>(char const *,char const *,char,int)
Whilst it should find or search for:
SomeClass::tprintf<T,... Targs>(char const *,T, ... Targs)
Questions
Is my conclusion correct? Is this expected behavior? Is there a Solution for this?