From the course: C++ Templates and the STL

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Impact on header files

Impact on header files - C++ Tutorial

From the course: C++ Templates and the STL

Impact on header files

- [Instructor] If you're familiar with defining classes in C++, you may try to define a template class in a header file, and the template class implementation in a separate file, as in this example. Here we have a great application for templates. It's a stack type, a stack class, and it will implement a very simple, fixed size LIFO stack using a template. So, it should work with any type. And we've got the header file here, with the interface, and we have a cpp file here with the implementation, and then we use it here from this template-class.cpp. Now, the problem with this is that, well, it doesn't work. When I tried to build, you'll notice that build fails. And the error I get here is linker command failed, with exit code one, and there's actually a very reasonable explanation for this. So we have the stack interface to find, or the class interface to find in this file, stack.h. And so the compiler processes the header file, and it generates specializations for the template class…

Contents