Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The linker is very clear : you are missing definition of those methods. The compiler doesn't complain, since it see the declaration of the Linked_List class, but when dealing with templates, that is not enough.</p> <p>In order for the template instantiation to be successful, all template definitions need to be visible. Since, you implemented methods of the template class in a cpp file (people usually do it in the header, or impl file), you need to include that file where the template is being instantiated. In your specific example, in the main.cpp :</p> <pre><code>#include &lt;iostream&gt; #include "Linked_List.cpp" // !!! int main(){ Linked_List&lt;int&gt; list; for (int i = 0; i &lt; 20; i++) { list.push_back(i); } for (int j = 0; j &lt; 20; j++) { std::cout &lt;&lt; list.pop_back() &lt;&lt; std::endl; } } </code></pre> <hr> <p>Instead of including the implantation file, and have the compiler implicitly instantiate template, you can do it explicitly in the source file. In this case, add this line at the end of Linked_List.cpp :</p> <pre><code>template class Linked_List&lt;int&gt;; </code></pre> <p>but in that case, you will not be able to create objects with different template argument (other than int).</p> <hr> <p>The third option is a variation of the first. Include the source file at the end of the header.</p> <p>Header</p> <pre><code>// header.hpp #ifndef HEADER_HPP #define HEADER_HPP template&lt; typename T &gt; struct A { void foo(); }; #include "source.impl" #endif </code></pre> <p>Source</p> <pre><code>// source.impl #include "header.hpp" template&lt; typename T &gt; void A::foo() { } </code></pre> <p>Main</p> <pre><code>// main.cpp #include "header.hpp" int main() { A&lt;int&gt; a; a.foo(); } </code></pre>
 

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