The problem you have here is that you have hidden the definition of the constructor in the .cpp file. This definition applies to all types T
, including T
as an int
that you use, but actually supplies no definitions whatsoever because its still only a declaration.
The linker cannot find the symbol Array<int>::Array()
.
Now, you can add a line like this:
Array<int> arr1;
to the end of your Array.cpp file and this makes the compiler instantiate the correct definition that the linker is looking for. However, this only supplies one definition, that of Array<int>
and no other.
This solution will work, until you need an Array
of a different template parameter, say, double
, at which point you would need to add:
Array<double> arr2;
to the end of your Array.cpp file - now you can see how this is unsustainable!
If you need C++ to work with any type that you might want in the future, now is the time to move the definition of the ctor (and presumably all the other member functions) up into the header (and delete the .cpp file as it won't have anything left in it).