#include <iostream>
#include "util.h"
int main()
{
std::cout << "Do you agree (Please enter 'y' or 'n'): ";
util::getConsoleInput('y', "Does not match!");
std::cout << "Enter default keycode: ";
util::getConsoleInput(1234, "Does not match!");
return 0;
}
But I am having this error:
C:/cpp-workspace/testDriver.cpp:9: undefined reference to `void util::getConsoleInput<char>(char, char const*)'
collect2.exe: error: ld returned 1 exit status
If I remove my header file and put everything in my testdriver.cpp, I don't have any issues but putting it in header files is encountering some problem.
Don't separate out template definitions into their own implementation file.
The reason for this is that the compiler must know the full definition of the template in order to generate a function.
In other words, move the contents of util.cpp into util.h, and delete util.cpp.
More details at: Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file? https://isocpp.org/wiki/faq/templates
Not just normal, it is required so the compiler knows how to generate the function at compile time. Even if the header is included into multiple source files.
That is how templates work in C++.
Well, you could manually copy'n'paste the block of templated code into every single source file that uses the function, but why not let the preprocessor do the pasting job for ya?