0
votes

I'm feeling rather stupid - must be missing something silly --- I wanted to subclass the generic STL stack class so that I could extend it with a combined top followed by pop operation but my compiler is telling me that both top and pop are undeclared identifiers.

template <typename T>
class MyStack : public stack<T>
{
   public:
      T PopTop()  // Convenience - combined top and pop in one go
         {
            T result = top();
            pop();
            return result;
         }
};

I note that I can fix this problem by writing

   T result = stack<T>::top();

but why isn't top seen automatically given the stack class is directly inherited?

Thanks in advance

1
this->top() also works. This happens whenever a base class depends on a template parameter. - HolyBlackCat
Be careful about inheriting from STL classes - they lack virtual destructors and are not intended to be inherited from. - StuporMundi

1 Answers

2
votes

First, don't descend from std classes, rather use private member (composition) and delegate functions - that's a common warning. With that said, this is because a base class with a template argument is not considered in ADL lookup regarding the descendant class. You might have using stack<T>::pop; and similar declarations for each function that you plan to use in MyStack.