【发布时间】:2019-02-09 18:59:16
【问题描述】:
所以我正在尝试创建一个从链表类继承成员函数的堆栈类。链表类没有自己的实际实现;它本质上是一个抽象的虚拟类。两者都是模板类。当我尝试使用派生的堆栈类访问成员函数时,我收到“没有在类'堆栈'中声明的成员函数”错误。下面是我的代码。我不确定问题是什么。我在堆栈类的声明中包含了 .h 文件的名称以及 : public List 序列。请帮忙!如果您需要更多代码来回答这个问题,请告诉我!!谢谢!
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 类的成员函数示例。 还包括页面顶部的 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) {
^
【问题讨论】:
-
minimal reproducible example 会有所帮助,我们不知道父类是如何声明的。
-
我在您的示例中没有看到任何 abot
bool Stack221<T>::clear()(检查您的代码 sn-p 中显示的错误)
标签: c++ templates inheritance