Click here to Skip to main content
15,845,212 members
Articles / Programming Languages / C++

How to Define a Template Class in a .h File and Implement it in a .cpp File

Rate me:
Please Sign up or sign in to vote.
4.47/5 (47 votes)
22 Dec 2009CPOL3 min read 563.5K   47  
This article suggests three methods to implement template classes in a .cpp file.
The common procedure in C++ is to put the class definition in a C++ header file and the implementation in a C++ source file. Then, the source file is made part of the project, meaning it is compiled separately. But when we implement this procedure for template classes, some compilation and linking problems will arise. This article looks at three possible solutions with examples: You can create an object of a template class in the same source file where it is implemented, you can #include the source file that implements your template class in your client source file, and you can #include the source file that implements your template class (TestTemp.cpp) in your header file that defines the template class (TestTemp.h), and remove the source file from the project, not from the folder.


Daily Counts


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions