How to call a template method?

There are many online documents explaining how to write template methods, but not many examples of calling them, how to use them in code.

I have a template method like this:

VectorConvertor.h

template <class T> static void AppendToVector(std::vector<T> & VectorToBeAppended, std::vector<T> & VectorToAppend); 


VectorConvertor.cpp

 template <class T> void VectorConvertor::AppendToVector(std::vector<T> & VectorToBeAppended, std::vector<T> & VectorToAppend) { for (std::vector::size_type i=0; i<VectorToAppend.size(); i++) { VectorToBeAppended.push_back(VectorToAppend.at(i)); } } 

Attempted use in code:

 std::vector<uint8_t> InputData, OutputData; // ... VectorConvertor::AppendToVector(OutputData, InputData); 


I compile this code without errors. But when I try to use this method, I get the following errors:

error LNK1120: 1 unresolved external

and

error LNK2019: unresolved external symbol "public: static void __cdecl VectorConvertor :: AppendToVector (class std :: vector> &, class std :: vector> &)" (? $ AppendToVector @E @VectorConvertor @@ SAXAEAV? $ vector @EV ? $ allocator @E @std @@@ std @@ 0 @Z) link in the function "public: staticclass std :: vector> __cdecl Utf8 :: WStringToUtf8 (class std :: basic_string, class std :: allocator>)" ( ? WStringToUtf8 @ Utf8 @@ SA? AV? $ Vector @EV? $ Allocator @E @std @@@ std @@ V? $ Basic_string @_WU? $ Char_traits @_W @std @@ V? $ Allocator @_W @ 2 @@ 3 @@ Z)


When I do not use this method in my code, I do not receive any error messages. What am I doing wrong when I call? Did I miss something?


I am using Visual Studio 2010 Express Edition.

+6
c ++ function methods templates
source share
5 answers

In C ++, you cannot separate the definition in a .cpp file when using templates. You must put the definition in the header file. Cm:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

+7
source share

You need to put the function body in the header file. See this FAQ .

+5
source share

Linking patterns can be tricky. But the simplest solution is usually:

Place all template definitions in header files.

In this case, you should move the contents of VectorConverter.cpp to VectorConverter.h (or perhaps #include "VectorConverter.cpp" at the bottom of VectorConverter.h).

+2
source share

Besides placing the definition of the template function in the header or including the source file (cpp file), you can explicitly create an instance of your function for the types you need.

0
source share

Errors result from trying to write a template method definition in a CPP file. To reduce the binding time, the rule is to write the implementation (definition) of built-in and template methods in the header file. The code below is the correct way to declare and define a template method.

VectorConvertor.h

 class VectorConvertor { public: template <class T> static void AppendToVector1( std::vector<T> & VectorToBeAppended, const std::vector<T> & VectorToAppend); template <class T> static void AppendToVector2( std::vector<T> & VectorToBeAppended, const std::vector<T> & VectorToAppend); template <class T> static void AppendToVector3( std::vector<T> & VectorToBeAppended, const std::vector<T> & VectorToAppend); } template <class T> void VectorConvertor::AppendToVector1( std::vector<T> & VectorToBeAppended, const std::vector<T> & VectorToAppend) { VectorToBeAppended.reserve(VectorToBeAppended.size() + VectorToAppend.size()); for (std::vector<T>::size_type i=0; i<VectorToAppend.size(); i++) { VectorToBeAppended.push_back(VectorToAppend[i]); } } template <class T> void VectorConvertor::AppendToVector2( std::vector<T> & VectorToBeAppended, const std::vector<T> & VectorToAppend) { VectorToBeAppended.reserve(VectorToBeAppended.size() + VectorToAppend.size()); for (std::vector<T>::const_iterator cit=VectorToAppend.cbegin(); cit!=VectorToAppend.cend(); ++cit) { VectorToBeAppended.push_back(*cit); } } template <class T> void VectorConvertor::AppendToVector3( std::vector<T> & VectorToBeAppended, const std::vector<T> & VectorToAppend) { VectorToBeAppended.insert(VectorToBeAppended.end(), VectorToAppend.cbegin(), VectorToAppend.cend()); } 

VectorConvertor.cpp

 // Nothing to write in the CPP file. 

And here is how to use it in the code:

 std::vector<uint8_t> InputData, OutputData; // ... VectorConvertor::AppendToVector3(OutputData, InputData); 
0
source share

All Articles