Note that there are some explanatory texts on larger screens.

plurals
  1. POTemplatize LinkedList/Stack, won't work!
    primarykey
    data
    text
    <p>I'm trying to tempalitize a linkedlist(/stack/queue) to accept int and double. But so far I can't get my code to work. (I took out the list/queue member functions to simply try and get it to work/not give errors when building. So it's mainly just the stack stuff.) Without the "template stuff" code works fine. If I missed anything or need to add anything LMK!</p> <p>Errors I get our:</p> <p>1>LinkedList_Stack.obj : error LNK2019: unresolved external symbol "public: __thiscall NumberList::~NumberList(void)" (??1?$NumberList@H@@QAE@XZ) referenced in function _main</p> <p>1>LinkedList_Stack.obj : error LNK2019: unresolved external symbol "public: void __thiscall NumberList::pop(int &amp;)" (?pop@?$NumberList@H@@QAEXAAH@Z) referenced in function _main</p> <p>1>LinkedList_Stack.obj : error LNK2019: unresolved external symbol "public: void __thiscall NumberList::push(int)" (?push@?$NumberList@H@@QAEXH@Z) referenced in function _main</p> <p>1>F:\Documents and Settings\Riotson\Desktop\LinkList\LinkedList_Stack_Queue_BNS11\Debug\LinkedList_Stack_Queue_BNS11.exe : fatal error LNK1120: 3 unresolved externals</p> <p>I read 3 different post on this site and tried to mimic that for tempaltizing (Where to place the 's and what not) but couldnt' get it to work =. Help please!!!</p> <p><strong>List.h:</strong></p> <pre><code>#ifndef LIST_H #define LIST_H template&lt;class T&gt; struct ListNode { T value; // Value in this node struct ListNode&lt;T&gt; *next; // Pointer to the next node }; // Class of LinkedList/Stack/Queue template&lt;class T&gt; class NumberList { private: ListNode&lt;T&gt; *head, *rear; // List head pointer public: //Constructor NumberList() { head = NULL; rear = NULL; } //Destructor ~NumberList(); // Linked List Operations void displayList() const; //Stack operations void push(T); void pop(T &amp;); bool isEmpty(); }; template&lt;class T&gt; void NumberList&lt;T&gt;::displayList() const { ListNode *nodePtr; nodePtr = head; while(nodePtr) { cout &lt;&lt; nodePtr-&gt;value &lt;&lt; endl; nodePtr = nodePtr-&gt;next; } } template&lt;class T&gt; NumberList&lt;T&gt;::~NumberList() { ListNode&lt;T&gt; *nodePtr; ListNode&lt;T&gt; *nextNode; // Position nodePtr at the head of the list nodePtr = head; // While nodePtr is not at the end of the list.... while(nodePtr != NULL) { // Save a pointer to the next node. nextNode = nodePtr-&gt;next; // Delete the current node. delete nodePtr; // Position nodePtr at the next node. nodePtr = nextNode; } } template&lt;class T&gt; void NumberList&lt;T&gt;::push(T num) { ListNode&lt;T&gt; *newNode; // Point to a new node // Allocate a new node and store num there. newNode = new ListNode; newNode-&gt;value = num; //If there are no nodes in the list // make newNode the first node. if(isEmpty()) { head = newNode; newNode-&gt;next = NULL; } else { newNode-&gt;next = head; head = newNode; } } template&lt;class T&gt; void NumberList&lt;T&gt;::pop(T &amp;num) { ListNode&lt;T&gt; *temp; // Temporary pointer // First make sure stack isn't empty. if(isEmpty())//Create a DynIntQueue object. { cout &lt;&lt; "The stack is empty.\n"; } else // pop value off the top of stack { num = head-&gt;value; temp = head-&gt;next; delete head; head = temp; } } // Member function isEmpty returns true if the stack/Queue //is empty, or false otherwise. template&lt;class T&gt; bool NumberList&lt;T&gt;::isEmpty() { bool status; if(!head) status = true; else status = false; return status; } #endif </code></pre> <p><strong>Main:</strong></p> <pre><code>#include "List.h" int main() { int catchVar; // To hold value popped off the stack // Create a Dynamic IntStack object. NumberList&lt;int&gt; stack; // Push values onto stack cout &lt;&lt; "Pushing 1, 2, 3\n"; stack.push(1); stack.push(2); stack.push(3); cout &lt;&lt; endl; // Pop the values off the stack cout &lt;&lt; "Popping...\n"; stack.pop(catchVar); cout &lt;&lt; catchVar &lt;&lt; endl; stack.pop(catchVar); cout &lt;&lt; catchVar &lt;&lt; endl; stack.pop(catchVar); cout &lt;&lt; catchVar &lt;&lt; endl; // Try to pop another value off the stack. cout &lt;&lt; "\nAttempting to pop again... " &lt;&lt;endl; stack.pop(catchVar); cout &lt;&lt; endl; // Templitization of a Double cout &lt;&lt; "Pushing a double of 1.5\n"; stack.push(1.5); cout &lt;&lt; endl; cout &lt;&lt; "Popping double 1.5...\n"; stack.pop(catchVar); cout &lt;&lt; catchVar &lt;&lt; endl; cout &lt;&lt; "Templitization Succesful\n" &lt;&lt; endl; cout &lt;&lt; endl; return 0; } </code></pre>
    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.
    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