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 时出错.
#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
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
……
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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28