继承问题?

问题描述 投票:-3回答:1

所以我试图创建一个从链表类继承成员函数的堆栈类。链表类没有自己的实际实现;它本质上是一个抽象的虚拟类。两者都是模板类。当我尝试使用我的派生堆栈类访问成员函数时,我收到“没有在类'Stack'中声明的成员函数”错误。以下是我的代码。我不确定是什么问题。我在堆栈类的声明中包含了.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) {
                         ^
c++ templates inheritance
1个回答
1
投票

您似乎已经提供了Stack221<T>::push的实现,但是您没有在类声明中声明该方法。

© www.soinside.com 2019 - 2024. All rights reserved.