C ++构造函数的异常说明符

时间:2014-02-22 19:59:14

标签: c++ g++

如何为类构造函数编写异常说明符。 在下面的示例中,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

1 个答案:

答案 0 :(得分:2)

构造函数定义中的异常说明符位置错误。它应该在参数列表之后,而不是在成员初始化列表之后。

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