If you want to write a template function or template class in header file, you may want to implement it in another file and then include the header file in main.cpp.
But that’s not workable. You will get error like
Undefined symbols for architecture x86_64:
"void test<int>(int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
That’s because when the complier instantiate a template function/class, it creates a new function/class with the correspoding aurgment type. And when the complier looks for the implementation of the new function/class, it can’t find it. So you will get errors above.
Two ways to solve this:
1. implement the template function/class in header file.
2. implement the template function/class in another file and include that file in the header file(sounds like same to implement directly in the header file.)
Or the stupid way:
Explicitly implement all the template instances you’ll need.
like:
template<class T>
class foo;
template<int>
class foo {...};
template<double>
class foo {...};
...