Note that there are some explanatory texts on larger screens.

plurals
  1. POerror C2079: 'stackNameSpace::StackNode<T>::value' uses undefined class
    primarykey
    data
    text
    <p>the interface of Stack.h</p> <pre><code>#include "stdafx.h" //use linkedlist to implement the stack //which is different from using the array to implement the stack #ifndef STACK_H #define STACK_H using namespace std; namespace stackNameSpace { template&lt;class T&gt; struct StackNode { T value; T min_value; //current local min value StackNode* next; }; typedef StackNode&lt;class T&gt;* StackNodePtr; template&lt;class T&gt; class Stack { private: StackNodePtr top; public: Stack(); Stack(const Stack&amp; a_stack); ~Stack(); bool empty() const; T pop(); void push(T the_value); T getMin(); }; } //end of namespace #endif </code></pre> <p>The implementation of the stack.h</p> <pre><code>#include "stdafx.h" //use linkedlist to implement the stack //which is different from using the array to implement the stack #ifndef STACK_CPP #define STACK_CPP #include &lt;iostream&gt; #include &lt;cstdlib&gt; #include "Stack.h" using namespace std; namespace stackNameSpace { template&lt;class T&gt; Stack&lt;T&gt;::Stack() : top(NULL) //here should be Stack&lt;T&gt; instead of Stack {} template&lt;class T&gt; Stack&lt;T&gt;::Stack(const Stack&amp; a_stack) { if (a_stack.top == NULL) { return NULL; } else { StackNodePtr currentOld = a_stack.top; //construct the top of the new stack StackNodePtr currentNew = new StackNode&lt;class T&gt;;//the struct currentNew-&gt;value = currentOld-&gt;value; currentNew-&gt;min_value = currentOld-&gt;min_value; top = currentNew; //contruct the rest node in the stack currentOld = currentOld-&gt;next; while (currentOld != NULL) { currentNew-&gt;next = new StackNode&lt;class T&gt;; currentNew = currentNew-&gt;next; currentNew-&gt;value = currentOld-&gt;value; currentNew-&gt;min_value = currentOld-&gt;min_value; currentOld = currentOld-&gt;next; } currentOld-&gt;next = NULL; } } template&lt;class T&gt; Stack&lt;T&gt;::~Stack() { T data; while (!empty()) { data = pop(); } } template&lt;class T&gt; bool Stack&lt;T&gt;::empty() const { return (top == NULL); } template&lt;class T&gt; T Stack&lt;T&gt;::pop() { if (empty()) { cout &lt;&lt; "Error: popping an empty stack.\n"; exit(1); } T result = top-&gt;value; StackNodePtr temp = new StackNode&lt;class T&gt;; temp = top; top = top-&gt;next; delete temp; return result; } template&lt;class T&gt; void push(T the_value) { StackNodePtr temp = new StackNode&lt;class T&gt;; temp-&gt;value = the_value; temp-&gt;min_value = min(the_value, getMin());//This is Much better //temp-&gt;min_value = top-&gt;min_value; //This is NOT secure, since top may be NULL temp-&gt;next = top; //update the top node top = temp; } template&lt;class T&gt; T getMin() { if (top == NULL) return INT_MAX; else { return top-&gt;min_value; } } } //end of namespace #endif </code></pre> <p>The function using the Stack class</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include "Stack.h" //this is not the &lt;stack&gt;, which is STL //using namespace std; //NOTE: this must be wrong! because can not use multiple namespace at the same time using namespace stackNameSpace; using std::cout; using std::endl; int main() { Stack&lt;int&gt; sWithMin; sWithMin.push(5); cout&lt;&lt; sWithMin.getMin() &lt;&lt; endl; sWithMin.push(4); cout&lt;&lt; sWithMin.getMin() &lt;&lt; endl; sWithMin.push(5); cout&lt;&lt; sWithMin.getMin() &lt;&lt; endl; sWithMin.push(3); cout&lt;&lt; sWithMin.getMin() &lt;&lt; endl; sWithMin.push(6); cout&lt;&lt; sWithMin.getMin() &lt;&lt; endl; return 0; } </code></pre> <p>When I compile the project, I get an error in main() that "error C2079: 'stackNameSpace::StackNode::value' uses undefined class 'stackNameSpace::T'"</p> <p>I can not figure out the reason why it has the error. Could anyone please help me?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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