不能将变量声明为抽象类型
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;
^
从我从这里的另一个线程中读到的内容来看,"您需要使用与基类中完全相同的参数类型定义函数。
我不知道为什么,但这只是没有在我的脑海中点击。
项目3.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
双名单.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;
相关文章:
- 如何定义一个纯抽象基类
- 将抽象基类中的所有纯虚函数定义为 varaidaic 模板
- 在C++单元测试上下文中,抽象基类是否应将其他抽象基类作为函数参数
- 用c++中的纯虚拟方法抽象模板类
- 我们可以在没有新实例化的情况下声明一个抽象方法来返回抽象超类中的子类对象吗
- 有没有一种方法可以使用SFINAE来检测一个类型是否实现了给定的抽象基类
- 如何抽象接口类
- 自定义异常中的用户定义的空构造函数,具有多个继承和抽象基类
- 如何保存指向抽象基类的指针/引用,但在 c++ 中仍然可以复制
- C++:在共享对象中调用抽象基类构造函数/未定义的符号
- 从抽象基类中获取重载函数
- 如何构建一个 Map,其中键是抽象基类(而不是值)
- 无法调用纯抽象基类超载
- 私下继承C++抽象基类是什么意思
- 将抽象派生类对象存储在基类向量中
- 如何在抽象基类中实现运算符+
- 如何为抽象基类创建模板实例化?
- 如何从其抽象母类上的指针初始化子类?
- 崩溃 __cxxabiv1::__cxa_pure_virtual () - vtable ptr 到抽象基类
- CRTP - 是否可以创建一个抽象基类?