【问题标题】:Inheritance issue?继承问题?
【发布时间】: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&lt;T&gt;::clear()(检查您的代码 sn-p 中显示的错误)

标签: c++ templates inheritance


【解决方案1】:

您似乎提供了Stack221&lt;T&gt;::push 的实现,但您没有在类声明中声明该方法。

【讨论】:

  • 推送功能是从父类继承而来的。派生类中应该不需要声明吧?
  • 如果你提供一个实现,你必须提供一个声明。如果你的意思是从父类继承方法,你为什么要提供上面的实现?
猜你喜欢
  • 1970-01-01
  • 2011-04-05
  • 2021-07-24
  • 2012-05-11
  • 2015-02-12
相关资源
最近更新 更多