主题:链式栈的问题
#include<iostream.h>
#include<assert.h>
template<class Type> class Stack;
template<class Type> class StackNode
{
friend class Stack<Type>;
private:
Type data;
StackNode<Type> *link;
StackNode(Type d=0,StackNode<Type> *l=NULL):data(d),link(l){}
};
template<class Type> class Stack
{
Stack(){top=NULL;}
~Stack();
void Push(const Type& item);
Type Pop();
Type GetPop();
int IsEmpty() const {return top==NULL;}
friend ostream & operator << (ostream& os,Stack<Type>& s);
private:
StackNode<Type> *top;
};
template<class Type> ostream & operator << (ostream& os,Stack<Type>& s)
{
Stack<Type> *p=s.top;int i=0;
while(p!=NULL)
os<<++i<<":"<<p->data<<endl;
return os;
}
template<class Type> Stack<Type>::~Stack()
{
StackNode<Type> *p;
while(top!=NULL){p=top;top=top->link;delete p;}
}
template<class Type> void Stack<Type>::Push(const Type& item)
{
top=new StackNode<Type>(item,top);
}
template<class Type> Type Stack<Type>::Pop()
{
asseet(!IsEmpty());
StackNode<Type> *p=top;
Type retvalue=p->data;
top=top->link;
delete p;
return retvalue;
}
template<class Type> Type Stack<Type>::GetPop()
{
assert(!IsEmpty());
return top->data;
}
int main()
{
Stack<int> s;
return 0;
}
这个就是书上的,编译完之后又2个错误:
D:\C++\2\2.cpp(56) : error C2248: 'Stack<int>::Stack<int>' : cannot access private member declared in class 'Stack<int>'
D:\C++\2\2.cpp(14) : see declaration of 'Stack<int>::Stack<int>'
D:\C++\2\2.cpp(56) : error C2248: 'Stack<int>::~Stack<int>' : cannot access private member declared in class 'Stack<int>'
D:\C++\2\2.cpp(15) : see declaration of 'Stack<int>::~Stack<int>'
我没找到哪的毛病,哪位能给指点一下,谢谢啦!
#include<assert.h>
template<class Type> class Stack;
template<class Type> class StackNode
{
friend class Stack<Type>;
private:
Type data;
StackNode<Type> *link;
StackNode(Type d=0,StackNode<Type> *l=NULL):data(d),link(l){}
};
template<class Type> class Stack
{
Stack(){top=NULL;}
~Stack();
void Push(const Type& item);
Type Pop();
Type GetPop();
int IsEmpty() const {return top==NULL;}
friend ostream & operator << (ostream& os,Stack<Type>& s);
private:
StackNode<Type> *top;
};
template<class Type> ostream & operator << (ostream& os,Stack<Type>& s)
{
Stack<Type> *p=s.top;int i=0;
while(p!=NULL)
os<<++i<<":"<<p->data<<endl;
return os;
}
template<class Type> Stack<Type>::~Stack()
{
StackNode<Type> *p;
while(top!=NULL){p=top;top=top->link;delete p;}
}
template<class Type> void Stack<Type>::Push(const Type& item)
{
top=new StackNode<Type>(item,top);
}
template<class Type> Type Stack<Type>::Pop()
{
asseet(!IsEmpty());
StackNode<Type> *p=top;
Type retvalue=p->data;
top=top->link;
delete p;
return retvalue;
}
template<class Type> Type Stack<Type>::GetPop()
{
assert(!IsEmpty());
return top->data;
}
int main()
{
Stack<int> s;
return 0;
}
这个就是书上的,编译完之后又2个错误:
D:\C++\2\2.cpp(56) : error C2248: 'Stack<int>::Stack<int>' : cannot access private member declared in class 'Stack<int>'
D:\C++\2\2.cpp(14) : see declaration of 'Stack<int>::Stack<int>'
D:\C++\2\2.cpp(56) : error C2248: 'Stack<int>::~Stack<int>' : cannot access private member declared in class 'Stack<int>'
D:\C++\2\2.cpp(15) : see declaration of 'Stack<int>::~Stack<int>'
我没找到哪的毛病,哪位能给指点一下,谢谢啦!