2

テンプレートを使用してスタックを実装しようとしています。私の質問は、この状況で親クラスの変数をどのように使用するのですか?

この場合、私のコンパイル エラーは次のとおりです。'top, a, size' はこのスコープで宣言されていません。

    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->or buffer<T>::before を使用する必要があります。

それで

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