When using templates all template code must reside in a header file. You can't have any of the template code(Declaration and Definition) in a source file.
So you have a few options,one would be to implement both your insertion_sort() and display() functions completely in your vector.h header file and that should work fine with templates.
The other way if you want to break up the declaration and definition of your template function is to use two header files like so.
SomeFile.hpp
1 2 3 4 5 6 7 8 9 10 11 12
|
#ifndef SOME_FILE_HPP
#define SOME_FILE_HPP
// We declare our template here
template <typename T>
void mySortFunction(std::vector<T>& vec);
// This line is key, we are basically saying put the contents of SomeFile.inl right where this
// include is.
#include "SomeFile.inl"
#endif
| |
SomeFile.inl (The .inl extension is a common extension used for template definition, but you can use really any extension you want as long as it is not a .cpp or .c file)
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#ifndef SOME_FILE_INL
#define SOME_FILE_INL
// Here is where we define our template function, we don't need to include SomeFile.hpp
// in this file because the contents in SomeFile.inl are basically just going to be copied and
// pasted into the SomeFile.hpp file at the #include line.
template <typename T>
void mySortFunction(std::vector<T>& vec)
{
// Do some stuff here.
}
#endif
| |
It's up to you what way you want to do it. Both are perfectly legal and have their uses, sometimes you can deal with everything being in a single header file, while other times you want the separation to make things more organized.
Also please ignore any syntax mistakes in the above examples if there is any, don't have access to a compiler here so was doing it all inline in the post ;p.