抛出c++中的关键字
Throws keyword in C++
谁能指出正确的方法来做这个程序与异常处理?这是一个堆栈程序。
1)#include<iostream>
2)#include<stdlib.h>
3)#include<string.h>
4)using namespace std;
5)
6)template<class T>
7)class Stack {
8)private:
9) int max;
10) int top;
12) T* items;
13)public:
14) Stack(int size) {
15) max = size;
16) top = -1;
17) items = new T[max];
18) }
19) ~Stack(){delete [] items;}
20)
21) void push(T data) throws Exception{
22) if(full()) {
23) throw new StackException("Out of space!");
24) }
25) items[++top] = data;
26) }
27) T pop(){
28) if(empty()) throws Exception {
29) throw new StackException("No more elements to delete");
30) }
31) return items[top--];
32) }
33)
34) bool full() { return top == max-1; }
35) bool empty() { return top == -1; }
36)};
37)
38)int main() {
39) try{
40) Stack<int> s(10);
41) s.push(1);
42) s.push(2);
43) cout<<s.pop()<<endl;
44) }
45) catch(StackException e){
46) cout<<e.what()<<endl;
47) }
48) return 0;
49)}
编辑:我得到以下错误。我是c++中异常处理的新手,想知道我是否做得对——
3stacks.cpp:20:18: error: expected ‘;’ at end of member declaration
3stacks.cpp:20:20: error: ‘throws’ does not name a type
3stacks.cpp:26:8: error: expected ‘;’ at end of member declaration
3stacks.cpp:26:10: error: ‘throws’ does not name a type
3stacks.cpp: In function ‘int main()’:
3stacks.cpp:44:8: error: expected type-specifier before ‘StackException’
3stacks.cpp:44:23: error: expected ‘)’ before ‘e’
3stacks.cpp:44:23: error: expected ‘{’ before ‘e’
3stacks.cpp:44:23: error: ‘e’ was not declared in this scope
3stacks.cpp:44:24: error: expected ‘;’ before ‘)’ token
您还没有在程序的任何地方定义StackException
。你必须自己创造它。还要从函数签名中去掉throws Exception
,因为您也从未定义过该类型(它被称为throw Exception
)。
此外,没有必要声明签名中可能出现的异常,但最好声明函数永远不会抛出(在c++ 11中使用noexcept
)。在文档中说明可能出现的异常。此外,您还遗漏了一个可能的bad_alloc
.
总而言之,剥离所有代码并从<stack>
中使用std::stack
并剥离那些C库。然而,这里有一个例子,你可以这样做:
template<class T>
class Stack {
private:
int max;
int top;
T * items;
public:
struct out_of_space{};
struct empty_stack{};
Stack(int size) {
if(size)
max = size;
top = -1;
items = new T[max];
}
~Stack(){delete[] items;}
void push(const T & data){
if(full()) {
throw out_of_space();
}
items[++top] = data;
}
T pop(){
if(empty()){
throw empty_stack();
}
return items[top--];
}
bool full() const { return top == max-1; }
bool empty() const { return top == -1; }
};
int main() {
try{
Stack<int> s(10);
s.push(1);
s.push(2);
cout<<s.pop()<<endl;
} catch(const Stack<int>::out_of_space& e){
cout<< "stack out of space" <<endl;
} catch(const Stack<int>::empty_stack & e){
cout<< "stack is empty" <<endl;
}
return 0;
}
要真正使用e.what()
,你必须自己实现它。或者你可以继承std::exception
,重载它,只捕获const std::exception&
。
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- C++中的"inline"关键字
- 如何确保C++函数在定义之前声明(如override关键字)
- 谷歌模拟和覆盖关键字
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- 如果全局变量默认是外部变量,为什么要添加"extern"关键字?
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 为什么"delete"关键字不删除节点?
- 在 c++ 中正确定义"this"关键字?
- 这个额外的关键字在这个 c++ 类声明中是什么意思?
- 在 typedef 内部使用 const 关键字和在 typedef 外部使用 const 关键字之间有区别吗?
- C++ - 为什么这里需要'template'关键字?
- C++函数的关键字?
- 使用 'typename' 关键字将非类型视为依赖上下文中的类型
- "friend"关键字在C++中是什么意思?
- 声明C++数组(带或不带 "new" 关键字)
- 对齐和对齐的实际用例C++关键字
- 在 c++ 中三元运算符中不允许继续(关键字)吗?
- 考虑到其他好处,关键字'auto'真的有助于简化调试C++吗?
- 在 Objective-C++ -> EXC_BAD_ACCESS Error 中使用"new"关键字