Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a linked list, templated Stack
    primarykey
    data
    text
    <p>I am trying to compile the code my instructor has given us (had to retype it, but I can't find any typos) and it will not compile. We will be using this code for later assignments so I want to make it work before we get there.</p> <p>It should simply make a linked-list based stack. I understand how the code works, and I have done templates before, but I can't figure out why it won't compile.</p> <p>First Stack.h</p> <pre><code>#ifndef STACK_H #define STACK_H //Stack definition file //Stack.h template&lt;class ItemType&gt; struct NodeType&lt;ItemType&gt;; //Line 9 template&lt;class ItemType&gt; class StackType { public: StackType(); ~StackType(); void MakeEmpty(); void Push(ItemType); void Pop(ItemType &amp;); bool IsEmpty() const; bool IsFull() const; ItemType Top(); private: NodeType* topPtr; }; template&lt;class ItemType&gt; struct NodeType&lt;ItemType&gt; { int info; NodeType&lt;ItemType&gt;* next; }; //Line 34 #include "Stack.cpp" #endif </code></pre> <p>Stack.cpp</p> <pre><code>//Stack implemenation #include &lt;iostream&gt; template&lt;class ItemType&gt; StackType&lt;ItemType&gt;::StackType() { //Line 5 topPtr=NULL; } template &lt;class ItemType&gt; StackType&lt;ItemType&gt;::~StackType() { //Line 11 MakeEmpty(); } template &lt;class ItemType&gt; void StackType&lt;ItemType&gt;::MakeEmpty() { NodeType&lt;ItemType&gt;* tempPtr; while (topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr-&gt;next; delete tempPtr; } } template &lt;class ItemType&gt; void StackType&lt;ItemType&gt;::Pop(ItemType &amp; item) { NodeType&lt;ItemType&gt;* tempPtr; item = topPtr-&gt;info; tempPtr = topPtr; topPtr = topPtr-&gt;next; delete tempPtr; } template&lt;class ItemType&gt; void StackType&lt;ItemType&gt;::Push(ItemType item) { NodeType&lt;ItemType&gt;* location; location = new NodeType&lt;ItemType&gt;; location-&gt;info = newItem; location-&gt;next = topPtr; topPtr = location; } template &lt;class ItemType&gt; bool StackType&lt;ItemType&gt;::IsEmpty() const { return (topPtr=NULL); } template &lt;class ItemType&gt; bool StackType&lt;ItemType&gt;::IsFull() const { return (false); } template&lt;class ItemType&gt; ItemType StackType&lt;ItemType&gt;::Top() { return topPtr-&gt;info; } </code></pre> <p>and main.cpp</p> <pre><code>#include &lt;iostream&gt; #include "Stack.h" using namespace std; int main () { int whatever; StackType&lt;int&gt; s; s.Push(10); s.Push(1); s.Pop(whatever); return 0; } </code></pre> <p>The errors I get are</p> <blockquote> <p>c:\users\geldhart\dropbox\cs210\stack\stack.h(9): <br> error C2143: syntax error : missing ';' before '&lt;'<br> c:\users\geldhart\dropbox\cs210\stack\stack.h(9): <br> error C2059: syntax error : '&lt;'<br> c:\users\geldhart\dropbox\cs210\stack\stack.h(34): <br> error C2753: 'NodeType' : partial specialization cannot match argument list for primary template<br> Stack.cpp<br> c:\users\geldhart\dropbox\cs210\stack\stack.cpp(5): <br> error C2143: syntax error : missing ';' before '&lt;'<br> c:\users\geldhart\dropbox\cs210\stack\stack.cpp(5): <br> error C4430: missing type specifier - int assumed. Note: C++ does not support default-int<br> c:\users\geldhart\dropbox\cs210\stack\stack.cpp(5): <br> error C2988: unrecognizable template declaration/definition<br> c:\users\geldhart\dropbox\cs210\stack\stack.cpp(5): <br> error C2059: syntax error : '&lt;'<br> c:\users\geldhart\dropbox\cs210\stack\stack.cpp(11):<br> error C2588: '::~StackType' : illegal global destructor<br> c:\users\geldhart\dropbox\cs210\stack\stack.cpp(11): <br> fatal error C1903: unable to recover from previous error(s); stopping compilation</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    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