I'm trying to create data structure for sorting data that will firstly receive any kind of object, i.e. int
, string
or MyCustomObject
and then perform other tasks.
// heap.h
template <typename T>
class Heap{
public:
Heap();
~Heap();
}
//heap.cpp
template <typename T>
Heap<T>::Heap(){}
template <typename T>
Heap<T>::~Heap(){}
...
It turns out that from the very beginning when I try to run Heap<int> test
it shows that classic message undefined reference to Heap<int>::Heap()
. I found out that one workaround is to add template <typename int>
at the end of the code.
However, if I try to do it with std::vector
I'm pretty sure I can run std::vector<MyCustomObject> test
which uses templates even without adding that line template<typename MyCustomObject>
beforehand.
I want to know:
- How would I perform this same behavior in my data structure of receiving "unknown" types?
- Why
std::vector
implementation works whereas mine don't? What's missing?