且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LinkedList 类实现不能将变量声明为抽象类型

更新时间:2022-11-22 20:02:36

你的 IList 是一个抽象类,有六个纯虚成员函数.为了创建派生实例(即LinkedList),您需要也在孩子内部实现这些功能.

Your IList is an abstract class with six pure virtual member functions. In order to create the instance of the derived one (i.e. LinkedList) you need to implement those functions inside the child as well.

class LinkedList : public IList
{
    // ..... other members

public:
    // ..... other members
    virtual int getCurrentSize() const override;
    bool isEmpty() const override { // implementation  }
    bool add(int newEntry) override {// implementation }
    bool remove(int anEntry) override { // implementation }
    bool contains(int anEntry) override { // implementation }
    void clear() override { // implementation}
};

还建议使用 override 说明符覆盖基类中的虚函数,以便编译器和读者都可以轻松识别它们,而无需查看基类.

Also recommended using the override specifier to override the virtual functions from the base class, so that both the compiler and the reader can easily recognize them, without looking into to the base.

其他问题:

  • Also note that your getCurrentSize() functions declaration in the child class (i.e. LinkedList) lacks a const.
  • Typo at cout << p -> data >> " "; should be cout << p->data << " ";
  • Why is "using namespace std;" considered bad practice?