Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing List and Stack manually with template
    text
    copied!<p>my data structure teacher gave us the task to implement lists and stacks manually and it worked just fine, but then we had to reimplement it to accept any type of parameters using template. Since then, I've been facing lots of different errors. Right now, I'm having a c2019 error, but I can't see what's wrong with my code. Tried this:<code>pilha.getTopo&lt;int&gt;();</code>, this: <code>Lista&lt;int&gt; lista;</code>, this: <code>typedef Lista&lt;int&gt; Lista_int;</code>, but no luck.</p> <p>Now, my code:</p> ######UPDATE: The code bellow is working <p>Listas.h</p> <pre><code>#pragma once #include "stdafx.h" #include &lt;iostream&gt; using namespace std; /*============================Node================================*/ template &lt;typename N&gt; class Node { N value; Node *next, *prev; public: Node(void) {}; Node(N _value){ this-&gt;value = _value; this-&gt;prev = NULL; this-&gt;next = NULL; }; ~Node(void) {}; void setValue(int _value) { this-&gt;value = _value; } void setNext(Node *_next) { this-&gt;next = _next; } void setPrev(Node *_prev) { this-&gt;previous = _prev; } int getValue() { return this-&gt;value; } Node *getNext() { return this-&gt;next; } Node *getPrev() { return this-&gt;prev; } }; /*===========================List===============================*/ template &lt;typename T&gt; class List { Node&lt;T&gt; *begin, *end; int list_size; public: List(void){ this-&gt;begin = NULL; this-&gt;end = NULL; this-&gt;list_size = 0; }; ~List(void) {}; template &lt;typename T&gt; void insertBegin(Node&lt;T&gt; node){ Node&lt;T&gt; *newNode = new Node&lt;T&gt;; *newNode = node; if(this-&gt;list_size == 0){ this-&gt;begin = this-&gt;end = newNode; this-&gt;list_size += 1; }else{ newNode-&gt;setNext(begin); this-&gt;begin = newNode; this-&gt;list_size += 1; } }; template &lt;typename T&gt; void removeBegin(Node&lt;T&gt; node){ Node&lt;T&gt; *newNode = new Node&lt;T&gt;; *newNode = node; if(this-&gt;list_size == 0){ this-&gt;begin = this-&gt;end = NULL; this-&gt;list_size = 0; }else{ if(begin == end) this-&gt;end = NULL; this-&gt;begin = newNode-&gt;getNext(); newNode = begin; newNode-&gt;setPrev(NULL); this-&gt;list_size -= 1; } }; template &lt;typename T&gt; void insertEnd(Node&lt;T&gt; node){ Node&lt;T&gt; *newNode = new Node&lt;T&gt;; *newNode = node; if(this-&gt;list_size == 0){ this-&gt;begin = this-&gt;end = newNode; this-&gt;list_size += 1; }else{ newNode-&gt;setPrev(end); this-&gt;end = newNode; this-&gt;list_size += 1; } }; template &lt;typename T&gt; void removeEnd(Node&lt;T&gt; node){ Node&lt;T&gt; *newNode = new Node&lt;T&gt;; *newNode = node; if(this-&gt;list_size == 0){ this-&gt;begin = this-&gt;end = NULL; this-&gt;list_size = 0; }else{ if(begin == end) this-&gt;end = NULL; this-&gt;end = newNode-&gt;getPrev(); newNode = end; newNode-&gt;setNext(NULL); this-&gt;list_size -= 1; } }; template &lt;typename T&gt; void exibeList(){ Node&lt;T&gt; *node; cout &lt;&lt; "Begin: " &lt;&lt; begin &lt;&lt; endl &lt;&lt; "End: " &lt;&lt; end &lt;&lt; endl &lt;&lt; "Size: " &lt;&lt; list_size &lt;&lt; endl &lt;&lt; endl; if(begin != NULL){ node = begin; do{ cout &lt;&lt; "Mem. adress: " &lt;&lt; &amp;node &lt;&lt; endl &lt;&lt; "Value: " &lt;&lt; node-&gt;getValue() &lt;&lt; endl &lt;&lt; "Previous: " &lt;&lt; node-&gt;getPrev() &lt;&lt; endl &lt;&lt; "Next: " &lt;&lt; node-&gt;getNext() &lt;&lt; endl &lt;&lt; endl; node = node-&gt;getNext(); }while(node != NULL); } }; }; /*===========================Stack==============================*/ template &lt;typename T&gt; class MyStack //: public List&lt;T&gt; { Node&lt;T&gt; *top, *next; int my_stack_size; public: MyStack&lt;T&gt;(void){ this-&gt;my_stack_size = 0; this-&gt;top = NULL; this-&gt;next = NULL; }; ~MyStack&lt;T&gt;(void) {}; template &lt;typename T&gt; void insertTop(Node&lt;T&gt; node){ Node&lt;T&gt; *newNode = new Node&lt;T&gt;; *newNode = node; if(this-&gt;my_stack_size == 0){ this-&gt;top = this-&gt;next = newNode; this-&gt;my_stack_size += 1; }else{ newNode-&gt;setNext(top); this-&gt;next = top; this-&gt;top = newNode; this-&gt;my_stack_size += 1; } }; template &lt;typename T&gt; void removeTop(){ Node&lt;T&gt; *node; if(this-&gt;my_stack_size &gt; 0){ node = top; this-&gt;top = next; delete node; this-&gt;my_stack_size -= 1; }else cout &lt;&lt; "Stack underflow." &lt;&lt; endl; }; template &lt;typename T&gt; void getTop(){ Node&lt;T&gt; *node = new Node&lt;T&gt;; node = top; if(node-&gt;getPrev() == NULL) cout &lt;&lt; node-&gt;getValue() &lt;&lt; endl; else cout &lt;&lt; "Error. Node isn't the first." &lt;&lt; endl; }; template &lt;typename T&gt; void show(){ Node&lt;T&gt; *node; cout &lt;&lt; "Top: " &lt;&lt; top &lt;&lt; endl &lt;&lt; "Next: " &lt;&lt; next &lt;&lt; endl &lt;&lt; "Size: " &lt;&lt; my_stack_size &lt;&lt; endl &lt;&lt; endl; if(top != NULL){ node = top; do{ cout &lt;&lt; "Mem. adress: " &lt;&lt; &amp;node &lt;&lt; endl &lt;&lt; "Value: " &lt;&lt; node-&gt;getValue() &lt;&lt; endl &lt;&lt; "Previous: " &lt;&lt; node-&gt;getPrev() &lt;&lt; endl &lt;&lt; "Next: " &lt;&lt; node-&gt;getNext() &lt;&lt; endl &lt;&lt; endl; node = node-&gt;getNext(); }while(node != NULL); } }; }; </code></pre> <p>and finally, Lista.cpp</p> <pre><code>// Lista.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Listas.h" #include &lt;iostream&gt; using namespace std; int _tmain(int argc, _TCHAR* argv[]) { /* ERROR C2019 //typedef Pilha&lt;int&gt; Pilha_int; //typedef Nodo&lt;int&gt; Nodo_int; // //Lista_int lista; //Pilha_int pilha; //Nodo_int nodo(25), nodo2(40), nodo3(55), nodo4(70); */ /* C2019 Pilha&lt;int&gt; pilha; Nodo&lt;int&gt; nodo(25), nodo2(40); /*error C2955: use of class template requires template argument list //Pilha pilha; //Nodo nodo(25), nodo2(40), nodo3(55), nodo4(70); pilha.insereTopo(nodo); pilha.getTopo(); //C2783 pilha.insereTopo(nodo2); pilha.getTopo(); //C2783 pilha.exibe(); //error C2783: could not deduce template argument for 'T' pilha.exibe&lt;int&gt;(); //C2019 system("pause"); return 0; } </code></pre> <p>Thanks in advance.</p>
 

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