C++派生类
我有一个类(Queue),它继承自一个名为 Stack 的类。 它是这样的:
template <class T> class Stack
{
public:
virtual const T pop();
LinkedList<T> lst;
};
template <class T> class Queue : public Stack<T>
{
public:
virtual const T pop();
};
template <class T> const T Queue<T>::pop()
{
const T val = lst[0];
return val;
}
编译器说“lst undecleared”......为什么?
I have a class (Queue) which inherits from a class named Stack.
it goes like this:
template <class T> class Stack
{
public:
virtual const T pop();
LinkedList<T> lst;
};
template <class T> class Queue : public Stack<T>
{
public:
virtual const T pop();
};
template <class T> const T Queue<T>::pop()
{
const T val = lst[0];
return val;
}
The compiler says "lst undecleared"...why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为
lst
是基类Stack
的成员,而基类Stack
是T
的依赖类型。在模板完全实例化之前,编译器无法检查依赖类型。您必须通过编写Stack::lst
让编译器知道lst
是此类基类的一部分。正如评论中提到的,::lst 这样看起来更明确。
this->lst
也是一个可行的解决方案。然而,人们可能会删除this
因为认为不必要。 StackBecause
lst
is a member of the base classStack<T>
which is a dependent type onT
. The compiler can't check dependent types until the template is fully instantiated. You have to let the compiler know thatlst
is part of such base class by writingStack<T>::lst
.As its mention in comments,
this->lst
is also a viable solution. However, people are likely to remove thethis
as seen unnecessary.Stack<T>::lst
seems more explicit in this way.尝试使用
this->lst
而不是lst
。Try
this->lst
instead oflst
.