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;
}
};
Run Code Online (Sandbox Code Playgroud)
I note that I can fix this problem by writing
T result = stack<T>::top();
Run Code Online (Sandbox Code Playgroud)
but why isn't top seen automatically given the stack class is directly inherited?
Thanks in advance
首先,不要从std类继承,而是使用私有成员(组合)和委托函数 - 这是一个常见的警告。
话虽如此,错误是因为在有关后代类的 ADL 查找中不考虑具有模板参数的基类。using stack<T>::pop;对于计划在 中使用的每个函数,您可能有类似的声明MyStack。