如何定义一个嵌入式类的方法,该方法返回外部类之外的枚举黑客?

时间:2017-12-28 18:29:25

标签: c++ enums

我正在尝试实现类似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}}值。如果我在界面内定义它就可以了。但我想在外面这样做。 (与实现分开的接口)。

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是保留的运算符名称。