Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call a template method?
    primarykey
    data
    text
    <p>There are a lot of online documents explaining how to write template methods, but not much example about how to call them, how to use them in code.</p> <p>I have a template method like this: <br /><br /> <strong>VectorConvertor.h</strong></p> <pre><code>template &lt;class T&gt; static void AppendToVector(std::vector&lt;T&gt; & VectorToBeAppended, std::vector&lt;T&gt; & VectorToAppend);</code></pre> <p><br /> <strong>VectorConvertor.cpp</strong> <pre><code>template &lt;class T&gt; void VectorConvertor::AppendToVector(std::vector&lt;T&gt; &amp; VectorToBeAppended, std::vector&lt;T&gt; &amp; VectorToAppend) { for (std::vector::size_type i=0; i&lt;VectorToAppend.size(); i++) { VectorToBeAppended.push_back(VectorToAppend.at(i)); } }</code></pre><br /></p> <p><strong>Usage attempt in code:</strong> <pre><code>std::vector&lt;uint8_t&gt; InputData, OutputData; // ... VectorConvertor::AppendToVector(OutputData, InputData);</code></pre><br /><br /></p> <p>I compile this code without any error. But when I try to use this method I get the following errors:</p> <blockquote> <p>error LNK1120: 1 unresolved externals</p> </blockquote> <p>and</p> <blockquote> <p>error LNK2019: unresolved external symbol "public: static void __cdecl VectorConvertor::AppendToVector(class std::vector > &amp;,class std::vector > &amp;)" (??$AppendToVector@E@VectorConvertor@@SAXAEAV?$vector@EV?$allocator@E@std@@@std@@0@Z) referenced in 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)</p> </blockquote> <p><br /></p> <p>When I don't use this method in my code I don't get any error messages. What am I doing wrong while calling it? Am I missing something?</p> <p><br /></p> <p>I'm using Visual Studio 2010 Express Edition.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload