+ -
当前位置:首页 → 问答吧 → help me!想用类模板简单实现Stack().结果出错了,求助!

help me!想用类模板简单实现Stack().结果出错了,求助!

时间:2011-12-04

来源:互联网

#ifdef _TSTACK_H_
#endif _TSTACK_H_
template <typename Type>
typedef struct SNode {
Type data;
SNode *next;
}SNode,*SNodePtr;

class Stack
{
private:
SNodePtr top;
public:
Stack();
~Stack();
bool Push(Type e);//入栈
bool Pop(Type &e);//出栈
bool IsEmpty(void);//判断是否空栈
void Clear(void);//清空栈
};

template <typename Type>
Stack<Type>::Stack()
{
top=NULL;
}

template <typename Type>
Stack<Type>::~Stack()
{
// Clear();
}

template <typename Type>
bool Stack<Type>::Push(Type e)
{
SNodePtr p=NULL;
p=new SNode;
if (p==NULL)
{
return false;
}
p->data=e;
p->next=top;
top=p;
return true;
}

template <typename Type>
bool Stack<Type>::Pop(Type &e)
{
if (top==NULL)
{
return false;
}

e=top->data;
SNodePtr p=top;
top=top->next;
delete p;
return true;
}

template <typename Type>
bool Stack<Type>::IsEmpty(void)
{
return top==NULL;
}

template <typename Type>
void Stack<Type>::Clear(void)
{
SNodePtr p;
while (top!=NULL)
{
p=top;
top=top->next;
delete p;
}
}
#endif

Compiling...
main.cpp
c:\users\chao\desktop\数据结构\template stack\tstack.h(7) : error C2059: syntax error : '<end Parse>'
c:\users\chao\desktop\数据结构\template stack\tstack.h(22) : error C2954: template definitions cannot nest
c:\users\chao\desktop\数据结构\template stack\tstack.h(23) : error C2039: '__ctor' : is not a member of 'Stack'
  c:\users\chao\desktop\数据结构\template stack\tstack.h(20) : see declaration of 'Stack'
c:\users\chao\desktop\数据结构\template stack\tstack.h(29) : error C2143: syntax error : missing ';' before '<'
c:\users\chao\desktop\数据结构\template stack\tstack.h(29) : error C2501: 'Stack' : missing storage-class or type specifiers
c:\users\chao\desktop\数据结构\template stack\tstack.h(29) : error C2373: 'Stack' : redefinition; different type modifiers
  c:\users\chao\desktop\数据结构\template stack\tstack.h(23) : see declaration of 'Stack'
c:\users\chao\desktop\数据结构\template stack\tstack.h(29) : error C2059: syntax error : ';'
c:\users\chao\desktop\数据结构\template stack\tstack.h(29) : error C2143: syntax error : missing ';' before '<'
c:\users\chao\desktop\数据结构\template stack\tstack.h(29) : error C2588: '::~Stack' : illegal global destructor
c:\users\chao\desktop\数据结构\template stack\tstack.h(29) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.

作者: shimachao   发布时间: 2011-12-04

C/C++ code
#ifndef _TSTACK_H_
#define _TSTACK_H_
template <typename Type>
struct SNode { //typedef去掉
Type data;
SNode *next;
}SNode,*SNodePtr;

template <typename Type> // 加上这一句
class Stack
{
private:
SNodePtr<Type> top; //这里。。。
public:
Stack();
~Stack();
bool Push(Type e);//入栈
bool Pop(Type &e);//出栈
bool IsEmpty(void);//判断是否空栈
void Clear(void);//清空栈
};

template <typename Type>
Stack<Type>::Stack()
{
top=NULL;
}

template <typename Type>
Stack<Type>::~Stack()
{
// Clear();
}

template <typename Type>
bool Stack<Type>::Push(Type e)
{
SNodePtr p=NULL;
p=new SNode;
if (p==NULL)
{
return false;
}
p->data=e;
p->next=top;
top=p;
return true;
}


template <typename Type>
bool Stack<Type>::Pop(Type &e)
{
if (top==NULL)
{
return false;
}

e=top->data;
SNodePtr p=top;
top=top->next;
delete p;
return true;
}

template <typename Type>
bool Stack<Type>::IsEmpty(void)
{
return top==NULL;
}

template <typename Type>
void Stack<Type>::Clear(void)
{
SNodePtr p;
while (top!=NULL)
{
p=top;
top=top->next;
delete p;
}
}
#endif

只是手工改了一下。手头没编译器,你试试。

作者: dizuo   发布时间: 2011-12-04

谢谢你,安你改后只要两个错误了
c:\users\chao\desktop\数据结构\template stack\tstack.h(7) : error C2143: syntax error : missing ';' before 'identifier'
c:\users\chao\desktop\数据结构\template stack\tstack.h(7) : fatal error C1004: unexpected end of file found
引用 1 楼 dizuo 的回复:
C/C++ code
#ifndef _TSTACK_H_
#define _TSTACK_H_
template <typename Type>
struct SNode { //typedef去掉
Type data;
SNode *next;
}SNode,*SNodePtr;

template <typename Type> // 加上这一句
class Stack
……

作者: shimachao   发布时间: 2011-12-04