I am implementing a dynamic array in c++ using a raw pointer. I'm new to using templates. I decided to split the class into two .hpp and .cpp files also for the sake of practice. I'm getting the following compilation error in the source file when initializing the T array:
"Allocation of incomplete type 'T'"
Here is the header file:
template <class T>
class DynArray {
public:
DynArray(); //default constructor
private:
T * array; //internal array
int memsize; //number of allocated memory slots
int t_size; //number of occupied memory slots
};
Here is the source file:
template <>
DynArray<class T>::DynArray() {
memsize = 2;
t_size = 0;
array = new T[memsize];
}
I'm not comfortable with what I'm doing here in the source file. I basically followed what my IDE (xcode) told me to do, but something feels wrong about creating a specialized template here. What is a correct way of doing this?