因此,我试图创建一个堆栈类,该堆栈类从链表类继承成员函数。链表类没有自己的实际实现。它本质上是一个抽象的虚拟类。两者都是模板类。当我尝试使用派生的堆栈类访问成员函数时,收到“在类'Stack'中未声明任何成员函数”错误。下面是我的代码。我不确定是什么问题。我在堆栈类的声明中包括了.h文件的名称以及:public List序列。请帮忙!如果您需要更多代码来回答这个问题,请告诉我!谢谢!
用于声明列表父类的代码
#ifndef LIST221_H
#define LIST221_H
#include "Node221.h"
template <typename T>
class List221 {
public:
List221();
~List221();
virtual int size() const;
virtual bool empty() const;
virtual bool push(T obj); //will push in a new node
virtual bool pop(); //will pop off the top node
virtual bool clear();
protected:
private:
Node<T>* front;
Node<T>* rear;
};
#endif
用于声明Stack类的代码。 包含List.h文件
#include "List221.h"
#include "Node221.h"
template <typename T>
class Stack221 : public List221 <T> {
public:
Stack221();
~Stack221();
T top();
private:
Node<T>* topnode;
};
我尝试访问的List类的Member函数的示例。 在页面顶部还包含List.h
template <typename T>
bool Stack221<T>::push(T obj) {
Node<T>* o = new Node(obj);
if (topnode == nullptr) {
topnode = o;
}
else {
o->next = topnode;
topnode = o;
}
return true;
}
显示错误
error: no ‘bool Stack221<T>::push(T)’ member function declared
in class ‘Stack221<T>’
bool Stack221<T>::push(T obj) {
^
答案 0 :(得分:1)
似乎您已经提供了Stack221<T>::push
的实现,但是您尚未在类声明中声明该方法。