Note that there are some explanatory texts on larger screens.

plurals
  1. POstruct with template object inside an templated class
    text
    copied!<p>I am trying to create a Templated class to hold different objects to lern a bit about selfwritten datastructures and ptrs which are new to me. I am stuck at this point:</p> <pre><code>template &lt;class T&gt; class Linked_List { struct Node { T data; Node* prev; Node* next; Node(T d, Node* p, Node* n) : data(d), prev(p), next(n) {} }; public: Linked_List(); ~Linked_List(); void push_back(T data); void push_front(T data); void insertAt(T data, int pos); T pop_back(); T pop_front(); int size(); bool empty(); private: Node* tail; Node* head; }; </code></pre> <p>I get this error: <code>'Linked_List&lt;T&gt;::Node::data' uses undefined class 'T'</code>. (using VS2013)</p> <p>What have i done wrong? </p> <p>Thanks </p> <hr> <p>Update:</p> <pre><code>#include "Linked_List.h" template&lt;class T&gt; Linked_List&lt;T&gt;::Linked_List() { head = 0; tail = 0; } template&lt;class T&gt; Linked_List&lt;T&gt;::~Linked_List() { { while (head) { Node* temp(head); //pointer to head head = head-&gt;next; // go to next delete temp; //delete the pointer } } } template &lt;class T&gt; void Linked_List&lt;T&gt;::push_back(T data) { //create the new Node tail = new Node(data, tail, NULL); if (tail-&gt;prev) //add to tail tail-&gt;prev-&gt;next = tail; if (empty()) // if empty its also the head head = tail; } template &lt;class T&gt; void Linked_List&lt;T&gt;::push_front(T data) { head = new Node(data, 0, tail); if (head-&gt;next) head-&gt;next-&gt;prev = head; if (empty()) tail = head; } template &lt;class T&gt; bool Linked_List&lt;T&gt;::empty() { return (!head || !tail); } template &lt;class T&gt; T Linked_List&lt;T&gt;::pop_back() { if (empty()) throw("Linked_list is empty!"); //pointer to the element we want to have Node* temp(tail); //set the tail to prev tail = tail-&gt;prev; //get the data T data = temp-&gt;data; //the prev does not have a next now if (tail) tail-&gt;next = 0; else head = 0; //delete the node which held our data delete temp; //return the data return data; } template &lt;class T&gt; T Linked_List&lt;T&gt;::pop_front() { if (empty()) throw("Linked_list is empty!"); //pointer to the element we want to have Node* temp(head); //set the tail to prev head = head-&gt;next; //get the data T data = temp-&gt;data; //the prev does not have a next now if (head) head-&gt;prev = 0; else tail = 0; //delete the node which held our data delete temp; //return the data return data; } </code></pre> <p>new stacktrace if i am trying to use it simply like this</p> <pre><code>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++) { cout &lt;&lt; list.pop_back() &lt;&lt; endl; } } </code></pre> <p>Error:</p> <pre><code> Error 1 error LNK2019: unresolved external symbol "public: __thiscall Linked_List&lt;int&gt;::Linked_List&lt;int&gt;(void)" (??0?$Linked_List@H@@QAE@XZ) referenced in function _main C:\Users\...\Documents\Visual Studio 2013\Projects\Double_Linked_List\Double_Linked_List\main.obj Double_Linked_List Error 2 error LNK2019: unresolved external symbol "public: __thiscall Linked_List&lt;int&gt;::~Linked_List&lt;int&gt;(void)" (??1?$Linked_List@H@@QAE@XZ) referenced in function _main C:\Users\...\Documents\Visual Studio 2013\Projects\Double_Linked_List\Double_Linked_List\main.obj Double_Linked_List Error 3 error LNK2019: unresolved external symbol "public: void __thiscall Linked_List&lt;int&gt;::push_back(int)" (?push_back@?$Linked_List@H@@QAEXH@Z) referenced in function _main C:\Users\...\Documents\Visual Studio 2013\Projects\Double_Linked_List\Double_Linked_List\main.obj Double_Linked_List Error 4 error LNK2019: unresolved external symbol "public: int __thiscall Linked_List&lt;int&gt;::pop_back(void)" (?pop_back@?$Linked_List@H@@QAEHXZ) referenced in function _main C:\Users\...\Documents\Visual Studio 2013\Projects\Double_Linked_List\Double_Linked_List\main.obj Double_Linked_List Error 5 1 error LNK1120: 4 unresolved externals C:\Users\...\Documents\Visual Studio 2013\Projects\Double_Linked_List\Debug\Double_Linked_List.exe 1 Double_Linked_List </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