C++抛出异常非法

C++ throwing exception illegal

出现错误:非法使用此类型名 这是 operator+ 重载:

    template<class T>
        inline Vec<T> Vec<T>::operator+(const Vec& rhs) const
        {
            int vecSize = 0;
            if (rhs.size() == 0 || size() == 0) {
                throw ExceptionEmptyOperand;
            }
            if (rhs.size() != size()) {
                throw ExceptionWrongDimensions;
            }
            else
            {
                Vec<T> vec;
                vecSize = rhs.size();
                for (int i = 0; i < vecSize;i++) {
                    vec.push(*this[i] + rhs[i])
            }

return vec;
        }

这是 operator[] 重载的声明:

  T& operator[](unsigned int ind);
    const T& operator[](unsigned int ind) const;

第一个是为了能够更改矢量值。

这是我尝试执行的操作,但出现了上述错误:

template<class T>
inline T& Vec<T>::operator[](unsigned int ind) 
{
    list<T>::iterator it = vals_.begin();
    if (size() == 0) {
        throw ExceptionEmptyOperand;
    }
    if (size() < ind) {
        throw ExceptionIndexExceed;
    }


    for (unsigned int i = 0; i<ind; i++) {
            ++it;
        }
        return *it;
    }

它给我这个错误:ExceptionEmptyOperand illegal use this type as an expression

template<class T>
    inline Vec<T> Vec<T>::operator+(const Vec& rhs) const
    {
        int vecSize = 0;
        if (rhs.size() == 0 || size() == 0) {
            throw ExceptionEmptyOperand;
        }
        if (rhs.size() != size()) {
            throw ExceptionWrongDimensions;
        }
        Vec<T> vec;
        vecSize = rhs.size();
        for (int i = 0; i < vecSize;i++) {
            vec.push((*this)[i] + rhs[i])
        return vec;
    }


template<class T>
inline T& Vec<T>::operator[](unsigned int ind) 
{
    if (size() == 0) {
        throw ExceptionEmptyOperand;
    }
    if (size() <= ind) {
        throw ExceptionIndexExceed;
    }

    list<T>::iterator it = vals_.begin();
    for (unsigned int i = 0; i<ind; i++) {
            ++it;
    }
    return *it;
}

template<class T>
inline const T& Vec<T>::operator[](unsigned int ind) const
{
    if (size() == 0) {
        throw ExceptionEmptyOperand;
    }
    if (size() <= ind) {
        throw ExceptionIndexExceed;
    }
    list<T>::const_iterator it = vals_.begin();

    for (unsigned int i = 0; i<ind; i++) {
        ++it;
    }
    return *it;
}

如果你有一个类型 ExceptionEmptyOperand 那么 throw ExceptionEmptyOperand; 是无效的语法。您需要创建一个该类型的对象,然后将其抛出:

throw ExceptionEmptyOperand();

// or
ExceptionEmptyOperand e;
throw e;