我试图实现一个类vec
看起来像class vector
一切正常,直到我试图定义一个embedded class method
返回外部类外的enum hack
值。
这是我的界面:
#define MAX 1000
template <class T>
class vec{
public:
// ctors, dtor, oper. over...
// Exeption handling
class CExcep {
public:
virtual void what()const = 0;
};
class COutBound : public CExcep {
public:
enum RANGE_ERROR {
NEGATIVE, TOO_BIG
};
COutBound(const int, const RANGE_ERROR);
const int getIndex()const;// { return index};
const RANGE_ERROR getError()const;// { return or ; }
virtual void what()const;
private:
const int index;
const RANGE_ERROR or;
};
private:
// some member data here
};
上面我在我的类CExcep
中嵌入了vec
基类,并且我使用继承来轻松地使用catch
通过base class
引用捕获异常。
所以问题:
如何在课堂COutBound::getError
之外定义vec
?
要做那样的COutBound::getIndex
,我设法做到了:
// ok
template<class T>
const int vec<T>::COutBound::getIndex()const{
return index;
}
但:
// Error here?
template<class T>
const vec<T>::COutBound::RANGE_ERROR
vec<T>::COutBound::getError()const{
return or;
}
简单地说getError
返回enum hack
类型的RANGE_ERROR
值。如果我在界面内定义它就可以了。但我想在外面这样做。 (与实现分开的接口)。
您需要将typename
用于RANGE_ERROR
,因为它是一个依赖类型:
template<class T>
typename vec<T>::COutBound::RANGE_ERROR
vec<T>::COutBound::getError()const{
return or;
}
或C ++ 11尾随返回:
template<class T> auto
vec<T>::COutBound::getError()const -> RANGE_ERROR {
return orx;
}
此外,简单返回类型的const
限定符也没有用,or
是保留的运算符名称。