簡體   English   中英

無法聲明變量為抽象類型

[英]cannot declare variable to be of abstract type

我必須制作一個繼承自DoubleListInterface.h的雙鏈表類

我正在編譯,但遇到了一個錯誤:

project3.cpp:19:21: error: cannot declare variable "list" to be of abstract type 'DoubleList<int>'
   DoubleList<int> list;
                   ^

從我從另一個線程上讀到的內容來看,“您需要使用與基類完全相同的參數類型來定義函數。”

我不知道為什么,但這只是沒有點擊我的腦袋。

project3.cpp:

#include <iostream>
#include <stdlib.h>
#include "DoubleList.cpp"

int failures = 0;

void test(int result, int expected) {
    if (result != expected) {
        std::cout << "test FAILED: expected(" << expected << ") but result(" << result << ")" << std::endl;
        failures++;
    }
}

int main(int argc, char **argv) {
    DoubleList<int> list;

    test(list.isEmpty(), true);
    test(list.getLength(), 0);

    list.insertFront(5);
    list.insertFront(7);
    list.insertBack(8);
    list.insertFront(2);

    test(list.isEmpty(), false);
    test(list.getLength(), 4);
    test(list.getEntry(1), 2);
    test(list.getEntry(2), 7);
    test(list.getEntry(3), 5);
    test(list.getEntry(4), 8);

    list.remove(1);
    test(list.getLength(), 3);
    test(list.getEntry(1), 7);
    test(list.getEntry(2), 5);
    test(list.getEntry(3), 8);

    list.remove(3);
    test(list.getLength(), 2);
    test(list.getEntry(1), 7);
    test(list.getEntry(2), 5);

    list.clear();
    test(list.isEmpty(), true);
    test(list.getLength(), 0);

    if (failures == 0)
        std::cout << "ALL TESTS PASSED" << std::endl;
    else
        std::cout << failures << " TESTS FAILED" << std::endl;

    return 0;
}

DoubleList.h:

#ifndef DOUBLE_LIST_
#define DOUBLE_LIST_

#include "DoubleListInterface.h"
#include "DoubleNode.cpp"

template<class ItemType>
class DoubleList : public DoubleListInterface<ItemType>
{
private:
    ItemType item;
    DoubleList<ItemType>* head;
    DoubleList<ItemType>* tail;

protected:
    // PUT DOUBLELIST METHODS HERE

public:
    // PUT INTERFACE METHODS HERE

    //DoubleList(const ItemType& anItem, DoubleList<ItemType>* headPtr, DoubleList<ItemType>* tailPtr);
    //~DoubleList(); <---------- Not sure if I need these or if I even implemented them right, so they're commented out for now

    bool isEmpty() const;
    int getLength() const;
    bool insertFront(const ItemType& newEntry);
    bool insertBack(const ItemType& newEntry);
    bool remove(int position);
    void clear();
    ItemType getEntry(int position);
};

#endif

DoubleList.cpp(到目前為止):

#include "DoubleListInterface.h"
#include "DoubleList.h"
#include <iostream>

int itemCount = 0;

template<class ItemType>
bool DoubleList<ItemType>::isEmpty() const
{
    if (this->head == nullptr)
        return true;
    else
        return false;
}

template<class ItemType>
int DoubleList<ItemType>::getLength() const
{
    return 0;
}

template<class ItemType>
bool DoubleList<ItemType>::insertFront(const ItemType& newEntry)
{
    return true;
}

DoubleListInterface.h:

template<class ItemType>
class DoubleListInterface
{
public:
    /** Sees whether this list is empty.
      @return True if the list is empty; otherwise returns false. */
    virtual bool isEmpty() const = 0;

    /** Gets the current number of entries in this list.
      @return The integer number of entries currently in the list. */
    virtual int getLength() const = 0;

    /** Inserts an entry into this list at the front.
      @pre  None.
      @post  If the insertion is successful, newEntry is at the front of
        the list, other entries are renumbered accordingly, and the returned value
        is true.
      @param newEntry  The entry to insert into the list.
      @return  True if insertion is successful, or false if not. */
    virtual bool insertFront(const ItemType& newEntry) = 0;

     /** Inserts an entry into this list at the back.
        @pre  None.
        @post  If the insertion is successful, newEntry is at the back of
          the list, other entries are renumbered accordingly, and the returned value
          is true.
        @param newEntry  The entry to insert into the list.
        @return  True if insertion is successful, or false if not. */
     virtual bool insertBack(const ItemType& newEntry) = 0;

    /** Removes the entry at a given position from this list.
      @pre  None.
      @post  If 1 <= position <= getLength() and the removal is successful,
        the entry at the given position in the list is removed, other
        items are renumbered accordingly, and the returned value is true.
      @param position  The list position of the entry to remove.
      @return  True if removal is successful, or false if not. */
    virtual bool remove(int position) = 0;

    /** Removes all entries from this list.
      @post  List contains no entries and the count of items is 0. */
    virtual void clear() = 0;

    /** Gets the entry at the given position in this list.
      @pre  1 <= position <= getLength().
      @post  The desired entry has been returned.
      @param position  The list position of the desired entry.
      @return  The entry at the given position. */
    virtual ItemType getEntry(int position) const = 0;

    virtual ~DoubleListInterface() { }
};

#endif

抱歉,如果很多的話,我現在真的迷路了。

ItemType getEntry(int position); 不覆蓋相應的DoubleListInterface方法。 注意缺少const限定詞。 您應該使用override說明符來避免此類問題

// Would trigger compilation error when base class
// does not have a virtual method with this exact signature.
ItemType getEntry(int position) override;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM