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;
            }
        };
6
  • 1
    It compiles for me, what other code are you using here? Commented Dec 21, 2015 at 1:38
  • 1
    i should be local variable and not member. Commented Dec 21, 2015 at 1:39
  • BTW, your inheritance is strange... You don't respect rule of 3/5/0. Commented Dec 21, 2015 at 1:41
  • 2
    Explained here Commented Dec 21, 2015 at 2:21
  • @meneldal I think the real qusetion is which MSVC version are you using ;) Commented Dec 21, 2015 at 2:21

1 Answer 1

4

To make them dependent name, you have to use this-> or buffer<T>:: before.

so

this->a = new T[siz];
this->size = siz;
this->top = -1;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.