我必须创建一个双链表列表,它继承自DoubleListInterface.h
我正在编译,但我遇到了错误:
rascal>parse(#start[Program], readFile(|project://rascal-test-library/src/hello.pico|), |project://rascal-test-library/src/hello.pico|)
start[Program]: (start[Program]) `begin declare input : natural,
output : natural,
repnr : natural,
rep : natural;
input := 14;
output := 1;
while input - 1 do
rep := output;
repnr := input;
while repnr - 1 do
output := output + rep;
repnr := repnr - 1
od;
input := input - 1
od
end`
从我在这里的另一个线程中读取的内容,"您需要使用与基类完全相同的参数类型来定义函数。"
我不知道为什么,但那只是没有点击我的脑袋。
project3.cpp:
project3.cpp:19:21: error: cannot declare variable "list" to be of abstract type 'DoubleList<int>'
DoubleList<int> list;
^
DoubleList.h:
#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.cpp(到目前为止):
#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
DoubleListInterface.h:
#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;
}
很抱歉,如果它很多,我现在真的迷失了。
答案 0 :(得分:2)
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;