我必须制作一个继承自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;
关于c++ - 无法声明变量为抽象类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47085062/