簡體   English   中英

C ++構造函數的異常說明符

[英]Exception specifier for C++ constructor

如何為類構造函數編寫異常說明符。 在下面的示例中,throw()正在處理除構造函數之外的所有其他成員函數。 我正在使用g ++ 4.8。

#include <exception>
#include <string>

class ArrayStackException: public std::exception {
public:
    ArrayStackException(std::string msg) : mMsg(msg) {}
    virtual const char* what() const throw () {
        return mMsg.c_str();
    }
    std::string mMsg;
};

template<typename T>
class ArrayStack {
public:
    //ArrayStack() = delete;
    explicit ArrayStack(int stackSize) throw();
    ~ArrayStack();
    void Push(T item)  throw();
    T Pop()  throw();
    bool IsEmpty() const;
    T StackTop() const  throw();

private:
    int mStackSize;
    int mTop;
    T* mItems;
};

template<typename T>
ArrayStack<T>::ArrayStack(int stackSize) : mStackSize(stackSize),
                                                       mTop(-1),
                                                       mItems(nullptr) throw() {
    if(stackSize < 1) {
        throw ArrayStackException(std::string("Stack size must be geater than 0"));
    }

    mItems = new T[mStackSize];
}

template<typename T>
ArrayStack<T>::~ArrayStack() {
    delete mItems;
}

template<typename T>
void ArrayStack<T>::Push(T item)  throw(){
    if(mTop == (mStackSize - 1)){
        throw ArrayStackException(std::string("Stack Overflow Occured..."));
    }

    ++mTop;
    mItems[mTop] = item;
}

template<typename T>
T ArrayStack<T>::Pop()  throw(){
    if(IsEmpty() == true){
        throw ArrayStackException(std::string("Stack Underflow Occured..."));
    }

    return mItems[mTop--];
}

template<typename T>
bool ArrayStack<T>::IsEmpty() const {
    return (mTop == -1 ? true : false);
}

template<typename T>
T ArrayStack<T>::StackTop() const  throw(){
    if(IsEmpty() == true) {
        throw ArrayStackException(std::string("Stack is empty!!"));
    }

    return mItems[mTop];
}



01:27:54 **** Incremental Build of configuration Debug for project ArrayStack ****
make all 
Building file: ../src/ArrayStack.cpp
Invoking: GCC C++ Compiler
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/ArrayStack.d" -MT"src/ArrayStack.d" -o "src/ArrayStack.o" "../src/ArrayStack.cpp"
In file included from ../src/ArrayStack.cpp:10:0:
../src/ArrayStack.h:31:40: error: declaration of ‘ArrayStack<T>::ArrayStack(int)’ has a different exception specifier
 ArrayStack<T>::ArrayStack(int stackSize) : mStackSize(stackSize),
                                        ^
../src/ArrayStack.h:17:11: error: from previous declaration ‘ArrayStack<T>::ArrayStack(int) throw ()’
  explicit ArrayStack(int stackSize) throw();
           ^
../src/ArrayStack.h: In constructor ‘ArrayStack<T>::ArrayStack(int)’:
../src/ArrayStack.h:33:33: error: expected ‘{’ before ‘throw’
                 mItems(nullptr) throw() {
                                 ^
../src/ArrayStack.h: At global scope:
../src/ArrayStack.h:33:33: error: expected unqualified-id before ‘throw’
make: *** [src/ArrayStack.o] Error 1

您在構造函數定義的錯誤位置具有異常說明符。 它應該在參數列表之后,而不是在成員初始化列表之后。

template<typename T>
ArrayStack<T>::ArrayStack(int stackSize) throw()
//                                  Here ^^^^^
 : mStackSize(stackSize), mTop(-1), mItems(nullptr) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM