LinkedList class 实现无法将变量声明为抽象类型
LinkedList class implementation cannot declare variable to be of Abstract Type
我在构建 LinkedList
class 时哪里出错了?
我已经在您的 LinkedList
class 中重新声明了来自 IList
的纯虚拟方法,但是 LinkedList
似乎被当作抽象 class 所以编译器似乎不允许我在我的 main
函数中创建一个 LinkedList
对象:
main.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
int main()
{
int A[] {1, 2, 3, 4, 5};
LinkedList l(A, 5);
cout << l.getCurrentSize()<<endl;
l.display();
return 0;
}
LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "IList.h"
class LinkedList : public IList
{
protected:
struct Node
{
int data;
struct Node* next;
};
struct Node* first;
public:
// constructor
LinkedList() { first = nullptr; }
LinkedList(int A[], int n);
// accessors void display();
virtual int getCurrentSize();
//destructor
virtual ~LinkedList();
};
#endif
LinkedList.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
// constructor
LinkedList::LinkedList(int A[], int n)
{
Node* last, * t;
int i = 0;
first = new Node;
first->data = A[0];
first->next = nullptr;
last = first;
for (i = 1; i < n; i++)
{
t = new Node;
t->data = A[i];
t->next = nullptr;
last->next = t;
last = t;
}
};
// destructor
LinkedList::~LinkedList()
{
Node* p = first;
while (first) {
first = first->next;
delete p;
p = first;
}
}
void LinkedList::display()
{
Node* p = first;
while (p)
{
cout << p->data >> " ";
p = p->next;
}
cout << endl;
}
int LinkedList::getCurrentSize() const
{
Node* p = first;
int len = 0;
while (p)
{
len++;
p = p->next;
}
return len;
}
IList.h
// Modified from code created by Frank M. Carrano and Timothy M. Henry.
// Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
#ifndef I_LIST_
#define I_LIST_
class IList
{
public:
/** Constructor */
IList () : traverseCount(0) { }
/** Destroys object and frees memory allocated by object.
(See C++ Interlude 2) */
virtual ~IList () { }
/** Gets the current number of entries in this list.
@return The integer number of entries currently in the list. */
virtual int getCurrentSize() const = 0;
/** Sees whether this list is empty.
@return True if the list is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this list.
@post If successful, newEntry is stored in the list and
the count of items in the list has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(int newEntry) = 0;
/** Removes one occurrence of a given entry from this list,
if possible.
@post If successful, anEntry has been removed from the list
and the count of items in the list has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(int anEntry) = 0;
/** Removes all entries from this list.
@post List contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Tests whether this list contains a given entry.
@param anEntry The entry to locate.
@return True if list contains anEntry, or false otherwise. */
virtual bool contains(int anEntry) = 0;
/** Get the count of number of nodes traversed.
@return The integer number of nodes traversed since last time the count was reset. */
virtual int getTraverseCount() const { return traverseCount; }
/** Reset the count of nodes traversed to zero. */
virtual void resetTraverseCount() { traverseCount = 0; }
protected:
int traverseCount;
}; // end IList
#endif
您的 IList
是一个抽象 class,具有六个纯虚成员函数。为了创建派生实例(即 LinkedList
),您需要 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
specifier 覆盖基础 class 的虚函数,这样编译器和 reader 都可以轻松识别它们,而无需查看基地.
其他问题:
- 另请注意,子 class(即
LinkedList
)中的 getCurrentSize()
函数声明缺少 const
.
cout << p -> data >> " ";
处的错字应为 cout << p->data << " ";
- Why is "using namespace std;" considered bad practice?
我在构建 LinkedList
class 时哪里出错了?
我已经在您的 LinkedList
class 中重新声明了来自 IList
的纯虚拟方法,但是 LinkedList
似乎被当作抽象 class 所以编译器似乎不允许我在我的 main
函数中创建一个 LinkedList
对象:
main.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
int main()
{
int A[] {1, 2, 3, 4, 5};
LinkedList l(A, 5);
cout << l.getCurrentSize()<<endl;
l.display();
return 0;
}
LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "IList.h"
class LinkedList : public IList
{
protected:
struct Node
{
int data;
struct Node* next;
};
struct Node* first;
public:
// constructor
LinkedList() { first = nullptr; }
LinkedList(int A[], int n);
// accessors void display();
virtual int getCurrentSize();
//destructor
virtual ~LinkedList();
};
#endif
LinkedList.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
// constructor
LinkedList::LinkedList(int A[], int n)
{
Node* last, * t;
int i = 0;
first = new Node;
first->data = A[0];
first->next = nullptr;
last = first;
for (i = 1; i < n; i++)
{
t = new Node;
t->data = A[i];
t->next = nullptr;
last->next = t;
last = t;
}
};
// destructor
LinkedList::~LinkedList()
{
Node* p = first;
while (first) {
first = first->next;
delete p;
p = first;
}
}
void LinkedList::display()
{
Node* p = first;
while (p)
{
cout << p->data >> " ";
p = p->next;
}
cout << endl;
}
int LinkedList::getCurrentSize() const
{
Node* p = first;
int len = 0;
while (p)
{
len++;
p = p->next;
}
return len;
}
IList.h
// Modified from code created by Frank M. Carrano and Timothy M. Henry.
// Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
#ifndef I_LIST_
#define I_LIST_
class IList
{
public:
/** Constructor */
IList () : traverseCount(0) { }
/** Destroys object and frees memory allocated by object.
(See C++ Interlude 2) */
virtual ~IList () { }
/** Gets the current number of entries in this list.
@return The integer number of entries currently in the list. */
virtual int getCurrentSize() const = 0;
/** Sees whether this list is empty.
@return True if the list is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this list.
@post If successful, newEntry is stored in the list and
the count of items in the list has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(int newEntry) = 0;
/** Removes one occurrence of a given entry from this list,
if possible.
@post If successful, anEntry has been removed from the list
and the count of items in the list has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(int anEntry) = 0;
/** Removes all entries from this list.
@post List contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Tests whether this list contains a given entry.
@param anEntry The entry to locate.
@return True if list contains anEntry, or false otherwise. */
virtual bool contains(int anEntry) = 0;
/** Get the count of number of nodes traversed.
@return The integer number of nodes traversed since last time the count was reset. */
virtual int getTraverseCount() const { return traverseCount; }
/** Reset the count of nodes traversed to zero. */
virtual void resetTraverseCount() { traverseCount = 0; }
protected:
int traverseCount;
}; // end IList
#endif
您的 IList
是一个抽象 class,具有六个纯虚成员函数。为了创建派生实例(即 LinkedList
),您需要 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
specifier 覆盖基础 class 的虚函数,这样编译器和 reader 都可以轻松识别它们,而无需查看基地.
其他问题:
- 另请注意,子 class(即
LinkedList
)中的getCurrentSize()
函数声明缺少const
. cout << p -> data >> " ";
处的错字应为cout << p->data << " ";
- Why is "using namespace std;" considered bad practice?