[英]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非法使用此類型作為表達式
如果您具有ExceptionEmptyOperand
類型,則throw ExceptionEmptyOperand;
是無效的語法。 您需要創建該類型的對象,然后將其拋出:
throw ExceptionEmptyOperand();
// or
ExceptionEmptyOperand e;
throw e;
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.