堆栈模板不编译push func

sil*_*eth 2 c++ stack templates g++

我写了类似堆栈的数据结构:

template<class T>
class Stos {
    class Element {
        public:
        T n;
        Element* prev;
        Element(const T& k = 0): n(k), prev(0) {}
    };
    Element* member;
    Stos(Stos&);
    public:
    Stos(const T&);
    ~Stos();
    unsigned int count;
    T get();
    Element* push(const T&);
    T pop();
    void mod(const T&);
};
Run Code Online (Sandbox Code Playgroud)

和实现(相同的文件):

template<class T>
Stos<T>::Stos(const T& n = 0): count(1) {
    member = new Element(n);
}

template<class T>
T Stos<T>::get() {
    return member->n;
}

template<class T>
Stos<T>::Element* Stos<T>::push(const T& n = 0) {
    Element* point = member;
    member = new Element;
    member->prev = point;
    if(n != 0) member->n = n;
    ++count;
    return member;
}

template<class T>
T Stos<T>::pop() {
    Element* point = member;
    T n = point->n;
    member = point->prev;
    --count;
    delete point;
    return n;
}

template<class T>
void Stos<T>::mod(const T& n) {
    member->n = n;
}

template<class T>
Stos<T>::~Stos() {
    while(member) pop();
}
Run Code Online (Sandbox Code Playgroud)

当我尝试用g ++编译它时,我得到关于第一行定义的错误Stos::Element* Stos::push():expected constructor, destructor, or type conversion before ‘*’ token.这是我第一次尝试用模板写一些东西.这个堆栈代码在没有模板的情况下工作,当我编辑它时,我得到了错误,一切正常,之前用"int"而不是"T".

我无法找出它为什么不编译.我不能使用指向class :: member的指针吗?

Jar*_*Par 5

您需要在名称前Element加上前缀typename

typename Stos<T>::Element* Stos<T>::push(const T& n = 0) 
Run Code Online (Sandbox Code Playgroud)

这里有一个完整的解释,说明为什么这是必要的