我正在尝试实现类似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
的{{1}}值。如果我在界面内定义它就可以了。但我想在外面这样做。 (与实现分开的接口)。
答案 0 :(得分:3)
您需要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
是保留的运算符名称。