07/08/2017 How to define a template class in a .h file and implement it in a .

cpp file - CodeProject
12,403,418 members (72,009 online) Sign in

home articles quick answers discussions features community help Search for articles, questions, tips

How to define a template class in a .h file and implement it in a .cpp file
Febil Chacko Thanikal, 22 Dec 2009 CPOL

4.30 (32 votes) Rate this:

This article describdes how to define a template class in a .h file and do its implementation in a .cpp file.

Introduction
This article suggests three methods to implement template classes in a .cpp file.

Background
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.

Compilation Issue
Here is some sample code:

Hide Shrink Copy Code

// TestTemp.h
#ifndef _TESTTEMP_H_
#define _TESTTEMP_H_

template<class T>
class TestTemp
{
public:
TestTemp();
void SetValue( T obj_i );
T Getalue();

private:

T m_Obj;
};
#endif

// TestTemp.cpp
#include "TestTemp.h"

TestTemp::TestTemp()
{
}
void TestTemp::SetValue( T obj_i )
{

}
T TestTemp::Getalue()
{
return m_Obj;
}

If you try to implement the template class like a normal class implementation as shown above, it will generate a set of compilation errors such as:

Hide Copy Code
: error C2955: 'TestTemp' : use of class template requires template argument list
: error C2065: 'T' : undeclared identifier

Reason

In this case, the compiler doesn't know about the object type. So it will not compile.

Solution

To compile this class without any errors, you need to put the template specific declaration in a .cpp file, as shown below:

Template Class Header File
Hide Copy Code

// TestTemp.h
#ifndef _TESTTEMP_H_

file:///opt/labuser/.cache/.fr-fxOFoi/ReferenceForTemplates/How-to-define-a-template-class-in-a-h-file-and-imp.html 1/6

#endif file:///opt/labuser/. }. So.CodeProject #define _TESTTEMP_H_ template<class T> class TestTemp { public: TestTemp(). not how to build the member functions. So. the compiler won't complain. #includeing the header file (TestTemp. if you have put the implementation in a source (TestTemp. } Linking Issue With the above code. you may get some link errors while you create an object of this class in any file other than TestTemp. the compiler will not be able to find it when it is trying to compile the client source file. you will get "unresolved references" to any of the class member functions that are not defined "inline" in the class definition. It will assume that these functions are provided elsewhere.cpp file . void SetValue( T obj_i ). And again.cpp #include "TestTemp. You can select from any of the methods below depending on which is suitable for your application design.. int.h" : TestTemp<int> TempObj.07/08/2017 How to define a template class in a . e. after resolving all the compilation errors. private: T m_Obj. : Link Error Hide Copy Code : error LNK2001: unresolved external symbol "public: __thiscall TestTemp<int>::TestTemp<int>(void)" (??0?$TestTemp@H@@QAE@XZ) Reason When the compiler encounters a declaration of a TestTemp object of some specific type.cpp).h) will not be sufficient at that time. #endif Template Class Source File Hide Copy Code // TestTemp.cpp) file and made it a separate part of the project. That only tells the compiler how to allocate for the object data and how to build the calls to the member functions.cpp #include "TestTemp. private: T m_Obj.cpp. Here is some sample code: Client Source File Hide Copy Code // Client. it must have access to the template implementation source. it will have no idea how to construct the TestTemp member functions. there is no need to link the object creation code with its actual implementation in some other file. T Getalue().fr-fxOFoi/ReferenceForTemplates/How-to-define-a-template-class-in-a-h-file-and-imp. Otherwise. when it's time to link. And. and leave it to the linker to find them. Solution There are different methods to solve this problem. T Getalue().g. Here is the sample code: Template Class Header File Hide Copy Code // TestTemp. This will cause the compiler to compile these particular types so the associated class member functions will be available at link time.h file and implement it in a . And.html 2/6 .h #ifndef _TESTTEMP_H_ #define _TESTTEMP_H_ template<class T> class TestTemp { public: TestTemp().cache/. }. Mehtod 1 You can create an object of a template class in the same source file where it is implemented (TestTemp. void SetValue( T obj_i ).h" template <class T> TestTemp<T>::TestTemp() { } template <class T> void TestTemp<T>::SetValue( T obj_i ) { } template <class T> T TestTemp<T>::Getalue() { return m_Obj.

cpp #include "TestTemp.h #ifndef _TESTTEMP_H_ #define _TESTTEMP_H_ template<class T> class TestTemp { public: TestTemp().CodeProject Template Class Source File Hide Copy Code // TestTemp. int nValue = TempObj.cpp" : TestTemp<int> TempObj. : Method 3 file:///opt/labuser/. Method 2 You can #include the source file that implements your template class in your client source file. void SetValue( T obj_i ). // it's just to avoid link error.h" template <class T> TestTemp<T>::TestTemp() { } template <class T> void TestTemp<T>::SetValue( T obj_i ) { } template <class T> T TestTemp<T>::Getalue() { return m_Obj.Getalue().Getalue().h" : TestTemp<int> TempObj.SetValue( 2 ). TempObj.cpp #include "TestTemp.h" #include "TestTemp. private: T m_Obj.cpp" will solve the link error. } Client Source File Hide Copy Code // Client. Here is the sample code: Template Class Header File Hide Copy Code // TestTemp.cpp #include "TestTemp. : The temporary function in "TestTemp. } Client Source File Hide Copy Code // Client. int nValue = TempObj. }. T Getalue().fr-fxOFoi/ReferenceForTemplates/How-to-define-a-template-class-in-a-h-file-and-imp. No need to call this function because it's global.SetValue( 2 ).07/08/2017 How to define a template class in a . void TemporaryFunction () { TestTemp<int> TempObj.html 3/6 .cpp #include "TestTemp. } // No need to call this TemporaryFunction() function.cache/.h" template <class T> TestTemp<T>::TestTemp() { } template <class T> void TestTemp<T>::SetValue( T obj_i ) { } template <class T> T TestTemp<T>::Getalue() { return m_Obj.cpp file . #endif Template Class Source File Hide Copy Code // TestTemp. TempObj.h file and implement it in a .

SetValue( 2 ).cpp #include "TestTemp.cache/.html 4/6 . }. #include "TestTemp.h #ifndef _TESTTEMP_H_ #define _TESTTEMP_H_ template<class T> class TestTemp { public: TestTemp(). along with any associated source code and files. TempObj.fr-fxOFoi/ReferenceForTemplates/How-to-define-a-template-class-in-a-h-file-and-imp.cpp #include "TestTemp.. T Getalue(). and remove the source file from the project. Tiny Template Library: implementing functor template #COBOLrocks TechCasts: New tricks for COBOL devs Pipe template classes F12 Edge Developer Tools file:///opt/labuser/. } Client Source File Hide Copy Code // Client. private: T m_Obj.. int nValue = TempObj. is licensed under The Code Project Open License (CPOL) Share EMAIL TWITTER FACEBOOK LINKEDIN REDDIT GOOGLE+ About the Author Febil Chacko ThanikalNo Biography provided Software Developer India India You may also be interested in.cpp" #endif Template Class Source File Hide Copy Code // TestTemp. not from the folder.Getalue().h" : TestTemp<int> TempObj.h).cpp) in your header file that defines the template class (TestTemp.07/08/2017 How to define a template class in a . Here is the sample code: Template Class Header File Hide Copy Code // TestTemp.h" template <class T> TestTemp<T>::TestTemp() { } template <class T> void TestTemp<T>::SetValue( T obj_i ) { } template <class T> T TestTemp<T>::Getalue() { return m_Obj. void SetValue( T obj_i ).h file and implement it in a .CodeProject You can #include the source file that implements your template class (TestTemp.cpp file . : License This article.

Ctrl+Up/Down to switch threads. A simple doubt Re: A simple doubt Re: A simple doubt Re: A simple doubt Re: A simple doubt Thanks a lot! Last Visit: 31-Dec-99 18:00 Last Update: 28-Jul-16 2:57 Refresh 1 General News Suggestion Question Bug Answer Joke Praise Rant Admin Use Ctrl+Left/Right to switch messages.9.3 on cygwin Re: Situation where Method 2 works but Method 3 fails on G++ 4. Ctrl+Shift+Left/Right to switch pages.CodeProject Microsoft's Guide to Modern Dev/Test Visual COBOL New Release: Small point. Permalink | Advertise | Privacy | Terms of Use | Mobile Layout: fixed | fluid Article Copyright 2009 by Febil Chacko Thanikal Web01 | 2.html 5/6 . Big deal Comments and Discussions First Prev Next Situation where Method 2 works but Method 3 fails on G++ 4.160721. 1999-2016 file:///opt/labuser/.9.h file and implement it in a .3 on cygwin error Method 4 (similar to method 2) Beginner's question My vote of 5 [My vote of 2] Please use the right vocabulary My vote of 5 And an elegant architecture for organizing C++ header files of template classes Method 4 (Another method) Re: Method 4 (Another method) Method 3 requires inline Nice article I don't like those solutions Some comments to your ideas: Re: Some comments to your ideas: I don't like any of these solutions.fr-fxOFoi/ReferenceForTemplates/How-to-define-a-template-class-in-a-h-file-and-imp.1 | Last Updated 22 Dec 2009 Everything else Copyright © CodeProject.07/08/2017 How to define a template class in a . Re: I don't like any of these solutions.8.cpp file .cache/.

h file and implement it in a .cpp file .CodeProject file:///opt/labuser/.07/08/2017 How to define a template class in a .cache/.html 6/6 .fr-fxOFoi/ReferenceForTemplates/How-to-define-a-template-class-in-a-h-file-and-imp.

Sign up to vote on this title
UsefulNot useful