c++ - error C2079: 'stackNameSpace::StackNode<T>::value' uses undefined class -


the interface of stack.h

#include "stdafx.h"  //use linkedlist implement stack  //which different using array implement stack   #ifndef stack_h #define stack_h  using namespace std;  namespace stacknamespace {  template<class t> struct stacknode {     t value;     t min_value; //current local min value      stacknode* next; };  typedef stacknode<class t>* stacknodeptr;  template<class t> class stack { private:       stacknodeptr top;  public:  stack();  stack(const stack& a_stack);  ~stack();  bool empty() const;  t pop();   void push(t the_value);  t getmin();  };  } //end of namespace  #endif 

the implementation of stack.h

#include "stdafx.h"  //use linkedlist implement stack  //which different using array implement stack   #ifndef stack_cpp #define stack_cpp #include <iostream> #include <cstdlib> #include "stack.h"  using namespace std;  namespace stacknamespace {   template<class t> stack<t>::stack() : top(null)  //here should stack<t> instead of stack {}  template<class t> stack<t>::stack(const stack& a_stack) {     if (a_stack.top == null) {         return null;     }     else {         stacknodeptr currentold = a_stack.top;         //construct top of new stack         stacknodeptr currentnew = new stacknode<class t>;//the struct          currentnew->value = currentold->value;         currentnew->min_value = currentold->min_value;         top = currentnew;          //contruct rest node in stack         currentold = currentold->next;         while (currentold != null) {             currentnew->next = new stacknode<class t>;             currentnew = currentnew->next;             currentnew->value = currentold->value;             currentnew->min_value = currentold->min_value;              currentold = currentold->next;         }         currentold->next = null;      } }  template<class t> stack<t>::~stack() {     t data;     while (!empty()) {         data = pop();     } }  template<class t> bool stack<t>::empty() const {     return (top == null); }  template<class t> t stack<t>::pop() {      if (empty()) {         cout << "error: popping empty stack.\n";         exit(1);     }      t result = top->value;      stacknodeptr temp = new stacknode<class t>;     temp = top;     top = top->next;      delete temp;      return result;  }  template<class t> void push(t the_value) {      stacknodeptr temp = new stacknode<class t>;     temp->value = the_value;      temp->min_value = min(the_value, getmin());//this better      //temp->min_value = top->min_value; //this not secure, since top may null      temp->next = top; //update top node     top = temp;  }  template<class t> t getmin() {     if (top == null)          return int_max;     else {         return top->min_value;     } }  } //end of namespace   #endif 

the function using stack class

#include "stdafx.h"  #include <iostream> #include "stack.h" //this not <stack>, stl   //using namespace std; //note: must wrong! because can not use multiple namespace @ same time using namespace stacknamespace; using std::cout; using std::endl;  int main() {      stack<int> swithmin;      swithmin.push(5);     cout<< swithmin.getmin() << endl;     swithmin.push(4);     cout<< swithmin.getmin() << endl;     swithmin.push(5);     cout<< swithmin.getmin() << endl;     swithmin.push(3);     cout<< swithmin.getmin() << endl;     swithmin.push(6);     cout<< swithmin.getmin() << endl;        return 0;  } 

when compile project, error in main() "error c2079: 'stacknamespace::stacknode::value' uses undefined class 'stacknamespace::t'"

i can not figure out reason why has error. please me?

namespace stacknamespace {  template<class t> struct stacknode {     t value;     t min_value; //current local min value      stacknode* next; }; 

so stacknode template depends on type parameter t.

typedef stacknode<class t>* stacknodeptr; 

this not part of template definition , class t refers class named t.

(actually class t refers class named t, except in construct template <class t>, replaced template <typename t>. template definition type parameter t type must referred using plain t, not class t.)

as haven't declared class named t yet, stacknodeptr definition implicitly declares incomplete class type @ surrounding namespace scope (i.e incomplete class type ::stacknamespace::t).

template<class t> class stack { private:       stacknodeptr top; 

now here stacknodeptr not dependent on template parameter t. instead pointer fixed type stacknode<::stacknamespace::t> , top->value of incomplete type class t unrelated template parameter of stack.

if use stack<int> , instantiate using top->value within such stack, you'll see error show.

btw: another, unrelated issue definitions of templates (including member functions of class templates) must visible @ point template instantiated. typically means should not put template member definition cppfile compiled separately. instead should in header file included wherever template used.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -