Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ about function templates
    primarykey
    data
    text
    <p>since 2 hours I struggle with templates, lets consider these files :</p> <h2>ChildrenOfBodom.h :</h2> <pre><code>#include&lt;iostream&gt; using namespace std; void AreYouDeadYet(string); template&lt;typename T1&gt;void InYourFace(T1); </code></pre> <h2>ChildreOfBodom.cc :</h2> <pre><code>#include"ChildrenOfBodom.h" void AreYouDeadYet(string s){ InYourFace(s); } template&lt;typename T1&gt;void InYourFace(T1 t1){ cout &lt;&lt; t1 &lt;&lt; endl; } </code></pre> <h2>main.cc :</h2> <pre><code>// g++ ChildrenOfBodom.cc main.cc #include"ChildrenOfBodom.h" int main(){ AreYouDeadYet("Awesome"); InYourFace("Awesome"); int i=8; InYourFace(i); return 0; } </code></pre> <p>at the compilation I got this message :</p> <pre><code>/tmp/ccN1fClI.o: In function `main': main.cc:(.text+0x54): undefined reference to `void InYourFace&lt;char const*&gt;(char const*)' main.cc:(.text+0x65): undefined reference to `void InYourFace&lt;int&gt;(int)' collect2: error: ld returned 1 exit status </code></pre> <p>my immediate solution was to write the function in <code>main.cc</code> (and still in <code>ChildrenOfBodom.cc</code>),</p> <h2>main.cc :</h2> <pre><code>#include"ChildrenOfBodom.h" template&lt;typename T1&gt;void InYourFace(T1 t1){ cout &lt;&lt; t1 &lt;&lt; endl; } int main(){ AreYouDeadYet("Awesome"); InYourFace("Awesome"); int i=8; InYourFace(i); return 0; } </code></pre> <p>this works. But I guess I store two times <code>InYourFace()</code>, so I did some searches and found what Tyler McHenry wrote (<a href="https://stackoverflow.com/questions/1353973/c-template-linking-error">here</a>), </p> <blockquote> <p>BlockquoteTemplates functions, including member functions, must be written entirely in headers.</p> </blockquote> <p>so I did it. But I would like to understand, because irc all of my teachers tell to don't write functions in a header. So is it really correct to write templates in headers ? or there is another way to write them in cc files, I mean a different way that one I used before, because doesn't sound really efficient if I need to change the template... and what does exactly the compiler when I use a template ? here I used a <code>string</code> and <code>int</code> so I guess the compiler create <code>InYourFace(int)</code> and <code>InYourFace(string)</code> and I guess if I call in <code>main.cc</code> thirty times <code>InYourFace(int)</code> it will not create thirty times <code>InYourFace(int)</code> but create only one and uses it thirty times. But if I use <code>InYourFace(int)</code> in <code>ChildrenOfBodom.cc</code>, does it will create <code>InYourFace(int)</code> too, or it will create <code>InYourFace(int)</code> one time only for <code>main.cc</code> and <code>ChildrenOfBodom.cc</code> ?</p> <p>Hope my question and my english are clear...</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