【问题标题】:Throws keyword in C++ [closed]在 C++ 中抛出关键字 [关闭]
【发布时间】:2023-04-08 00:45:01
【问题描述】:

有人能指出用异常处理来执行这个程序的正确方法吗?这是一个堆栈程序。

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

【问题讨论】:

  • 有什么提示吗?你面临什么问题?你试过什么?
  • “这是我的代码,修复它”。嗯:不。您对异常有任何疑问吗?顺便说一句:你在混合 C 和 C++ 头文件吗?
  • 这不是 Java 伙伴。
  • 使用异常规范是not recommended

标签: c++ exception-handling stack throw


【解决方案1】:

您尚未在程序中的任何位置定义StackException。您必须自己创建它。还要从你的函数签名中去除throws Exception,因为你也从未定义过该类型(它被称为throw Exception)。

此外,实际上没有必要在签名中说明可能的异常,但最好说明函数永远不会抛出(在 C++11 中使用 noexcept)。在文档中说明可能的例外情况。此外,您错过了一个可能的bad_alloc

总而言之,剥离所有代码并使用&lt;stack&gt; 中的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&amp;

【讨论】:

  • 我想自己实现一个,所以我没有使用libraray..
  • @Fox:我刚刚为这种情况添加了一个示例。如果您需要使用任何 C 头文件,请不要使用 library.h,而是使用 clibrary,例如cstdlib。顺便说一句,你在pop 中的逻辑被打破了。作业:为什么--top 正确而top-- 错误?
  • 非常感谢 .. top-- 是正确的,因为您必须返回元素,然后减少位置 ..
  • @Fox:糟糕,我不小心忘记了您使用top = -1 作为初始值。在这种情况下top-- 是正确的,是的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 2013-07-12
  • 2010-11-06
  • 1970-01-01
  • 2016-06-05
相关资源
最近更新 更多