2013-03-28 18 views
-5

有人可以通過異常處理指向正確的方式來執行此程序嗎?這是一個堆棧程序。在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 
+1

任何提示什麼是錯的?你面臨什麼問題?你有什麼嘗試? –

+3

「這是我的代碼,修復它」。好吧:不。你對異常有任何疑問嗎? btw:你在混合使用C和C++頭文件嗎? – Zeta

+1

這不是Java好友。 – 0x499602D2

回答

1

你有沒有在你的程序中定義StackException任何地方。你必須自己創建它。從您的函數簽名中刪除throws Exception,因爲您從未定義過該類型(並且它的名稱爲throw Exception)。

此外,在簽名中說明可能有哪些異常是沒有必要的,但最好說明函數永遠不會拋出(在C++ 11中使用noexcept)。在文檔中說明可能的例外。此外,你錯過了一個可能的bad_alloc

總而言之,將所有代碼剝離並使用std::stack<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&

+0

我想實現一個我自己,這就是爲什麼我沒有使用libraray .. – Fox

+0

@Fox:我剛剛爲這種情況添加了一個例子。如果你需要使用任何C頭文件,不要使用'library.h',而是使用'clibrary',例如'cstdlib'。順便說一句,你在'流行'的邏輯被打破了。作業:爲什麼'--top'正確,'top --'錯誤? – Zeta

+0

非常感謝.. top--是正確的,因爲你必須返回元素,然後遞減位置。 – Fox