2

I've been trying to use template to implement stack. And my question is how do I use the variables from the parent class in this situation?

In this case my compile error is: 'top, a, size' was not declared in this scope.

    template<class T>
        class buffer
        {
  public:
            T *a;
            int top,i,size;
        };

    template<class T>
        class Queue: public buffer<T>
        {
    public:
            Queue(int siz)
            {
                a=new T[siz];
                size=siz;
                top=-1;
            }
            void push(T ele)
            {
                if(top!=size-1){a[++top]=ele;} 
            }

            T pop()
            {
                  return(a[top--]);
            }

            void print()
            {
                for(i=0;i<top;i++)
                    cout<<" "<<a[i];

                cout<<endl;
            }
        };
4

1 回答 1

4

要使它们成为依赖名称,您必须使用this->orbuffer<T>::之前。

所以

this->a = new T[siz];
this->size = siz;
this->top = -1;
于 2015-12-21T01:35:40.497 回答