So I have this code:
//HashTable.hpp:
template <class dataType>
class HashTable
{
public:
//Something
public:
HashTable();
HashTable(unsigned int & tableSize);
//HashTable.cpp:
#include "HashTable.hpp"
template <class dataType>
HashTable<dataType>::HashTable()
{
}
Now, in main() body I just instantiate an object HashTable<int> hTable
. When compiling with g++, I get this error :
`EntryPoint.cpp:(.text+0x1f): undefined reference to `HashTable<int>::HashTable() '
collect2: error: ld returned 1 exit status`
What causes this problem? I recall I've hit this wall a few months ago when doing some templated data structures, but I just quick-fixed it in an error-prone way by including the file.cpp in the entry point's .cpp file. How can this be solved in a clean way? I've tried creating the object files and linking them individually, but I just get the same error. This specific problem frustrated me in such a way(lost > 10 hours researching it) that I've just stopped doing templates. I do not want to force the functions as inlines, as that's a very messy way of solving this. Also, I would avoid including HashTable.cpp instead of HashTable.hpp, as this overrides the concept of header files. I want my class to be as abstract as possible, so I can use it with all data types that I will later create.
Also, I do know that this can be solved by explicit instatiation, but I want to be able to use it with any types without having to "bloat" the code.