不知道是不是一个类型问题

zgjxwl 2008-03-24 04:49:59
这是一个堆栈的顺序表示里实现的一些基本运算,编译总是出错误,我就发个主函数,错误我放在下面了,大家帮我看看了
就是那个异常处理那里出问题了,按道理说是catch可以捕获的类型的,但不知道怎么使用,不知道是不是要包含什么头文件
我不知道要包含什么头文件,算了,大家看发上来的那个错误就知道了
/main.cpp
#include <iostream>
#include "Stack.h"
using namespace std;
void main()
{
try
{
SeqStack<double> dstk(2); //构造一个容量为2的双精度数栈
dstk.Push(3.7);
dstk.Push(8.8); //向栈中依次加入3.7和8.8
cout<<dstk; //输出栈中元素
dstk.Pop();
if(dstk.IsEmpty()) //判定栈是否为空
cout<<"Is empty!"<<endl;
else
cout<<"Is not empty!"<<endl;
dstk.Pop(); //两次删除运算,将产生下溢异常
dstk.Pop();
}
catch(ResultCode err)
{
switch(err) //根据异常类别,处理异常
{
case Overflow:
cout<<"Overflow!"<<endl;
break;
case Underflow:
cout<<"Underflow!"<<endl;
break;
}
}
}


错误:
--------------------Configuration: 堆栈的顺序表示练习 - Win32 Debug--------------------
Compiling...
main.cpp
E:\数据结构\第三章\堆栈的顺序表示练习\main.cpp(21) : error C2061: syntax error : identifier 'ResultCode'
E:\数据结构\第三章\堆栈的顺序表示练习\main.cpp(21) : error C2310: catch handlers must specify one type
E:\数据结构\第三章\堆栈的顺序表示练习\main.cpp(23) : error C2065: 'err' : undeclared identifier
E:\数据结构\第三章\堆栈的顺序表示练习\main.cpp(25) : error C2065: 'Overflow' : undeclared identifier
E:\数据结构\第三章\堆栈的顺序表示练习\main.cpp(25) : error C2051: case expression not constant
E:\数据结构\第三章\堆栈的顺序表示练习\main.cpp(28) : error C2065: 'Underflow' : undeclared identifier
E:\数据结构\第三章\堆栈的顺序表示练习\main.cpp(28) : error C2051: case expression not constant
E:\数据结构\第三章\堆栈的顺序表示练习\main.cpp(31) : warning C4060: switch statement contains no 'case' or 'default' labels
E:\数据结构\第三章\堆栈的顺序表示练习\main.cpp(33) : error C2317: 'try' block starting on line '8' has no catch handlers
执行 cl.exe 时出错.
...全文
59 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgjxwl 2008-03-24
  • 打赏
  • 举报
回复
#include "Stack.h"
template <class T>

//Stack.cpp
//构造函数的实现
SeqStack<T>::SeqStack(int mSize)
{
maxSize=mSize;
s=new T[maxSize];
top=-1;
}

//进栈函数的实现
template <class T>
void SeqStack<T>::Push(const T &x)
{
if (IsFull()) throw Overflow;
s[++top]=x;
}

//出栈函数的实现
template <class T>
void SeqStack <T>::Pop()
{
if (IsEmpty()) throw Underflow;
top--;
}

//返回栈顶元素的函数的实现
template <class T>
T SeqStack <T>::Top()const
{
if (IsEmpty()) throw Underflow;
return s[top];
}

//输出栈中所有元素的函数实现
template <class T>
void SeqStack <T>::Output(ostream &out)const
{
int i=top;
out<<endl<<"Stack contains:";
while (i!=-1) out<<s[i--]<<" ";
out<<endl;
}

//
template <class T>
ostream &operator<<(ostream& out,const SeqStack<T>&s)
{
s.Output(out);
return out;
}
zgjxwl 2008-03-24
  • 打赏
  • 举报
回复
//Stack.h
#include <ostream>
//ostream是包含在头文件ostream里的,也是标志符,要使用命名空间或者std::ostream才可以用
using namespace std;
//基类Stack的定义
template <class T>
class Stack
{
public:
virtual void Push(const T&x)=0;
virtual void Pop()=0;
virtual T Top()const=0;
virtual bool IsEmpty()const=0;
virtual bool IsFull()const=0;
};

//派生类SeqStack的定义
template <class T>
class SeqStack:public Stack<T>
{
public:
SeqStack(int mSize);
~SeqStack(){delete []s;}
bool IsEmpty() const {return (top==-1);}
bool IsFull() const {return (top==maxSize-1);}
void Push(const T&x);
void Pop();
T Top()const;
private:
void Output(ostream& out)const;
T *s;
int maxSize;
int top;
friend ostream &operator<<(ostream &out,const SeqStack<T>&s);
};

64,679

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧