双链接列表模板错误(C++)

Doubly Linked List Template Errors (C++)

本文关键字:C++ 错误 链接 列表      更新时间:2023-10-16

我正在为我的编程类做一项作业,我们必须编写一个双链表,然后将其转换为模板双链表。我使用了非模板版本,但现在我不知道如何修复模板版本上的错误。我会先把代码放在这里,然后在下面谈谈我的错误。

模板DoublyLinkedList.h

#include <cstdlib>
#include <iostream>
using namespace std;
template <typename T>
class DoublyLinkedList; // class declaration
// list node
template <typename T>
class DListNode {
private: T obj;
DListNode *prev, *next;
friend class DoublyLinkedList<T>;
public:
DListNode(T object = T(), DListNode *p = NULL, DListNode *n = NULL)
: obj(object), prev(p), next(n) {}
T getElem() const { return obj; }
DListNode * getNext() const { return next; }
DListNode * getPrev() const { return prev; }
};
// doubly linked list
template <typename T>
class DoublyLinkedList {
protected: DListNode header, trailer;
public:
DoublyLinkedList() : header(0), trailer(0) // constructor
{ /*header.next = &trailer; trailer.prev = &header;*/ }
DoublyLinkedList(const DoublyLinkedList<T>& dll); // copy constructor
~DoublyLinkedList(); // destructor
DoublyLinkedList& operator=(const DoublyLinkedList<T>& dll); // assignment operator
// return the pointer to the first node
DListNode *getFirst() const { return header.next; } 
// return the pointer to the trailer
const DListNode *getAfterLast() const { return &trailer; }
// return if the list is empty
bool isEmpty() const { return header.next == &trailer; }
T first() const; // return the first object
T last() const; // return the last object
void insertFirst(T newobj); // insert to the first of the list
T removeFirst(); // remove the first node
void insertLast(T newobj); // insert to the last of the list
T removeLast(); // remove the last node
};
// output operator
template <typename T>
ostream& operator<<(ostream& out, const DoublyLinkedList<T>& dll);
// extend range_error from <stdexcept>
template <typename T>
struct EmptyDLinkedListException : std::range_error {
explicit EmptyDLinkedListException(char const* msg=NULL): range_error(msg) {}
};
// copy constructor
template <typename T>
DoublyLinkedList<T>::DoublyLinkedList(const DoublyLinkedList<T>& dll)
{
header.next = &trailer; trailer.prev = &header;
DListNode<T>* current = dll.getFirst();
while (current != dll.getAfterLast()) {
T newObj = current->obj;
insertLast(newObj);
current = current->getNext();
}
}
// assignment operator
template <typename T>
DoublyLinkedList<T>& DoublyLinkedList<T>::operator=(const DoublyLinkedList<T>& dll)
{
delete this;
header.next = &trailer; trailer.prev = &header;
DListNode<T>* current = dll.getFirst();
while (current != dll.getAfterLast()) {
T newObj = current->obj;
insertLast(newObj);
current = current->getNext();
}
return *this;
}
// insert the object to the first of the linked list
template <typename T>
void DoublyLinkedList<T>::insertFirst(T newobj)
{ 
DListNode<T> *newNode = new DListNode(newobj, &header, header.next);
header.next->prev = newNode;
header.next = newNode;
}
// insert the object to the last of the linked list
template <typename T>
void DoublyLinkedList<T>::insertLast(T newobj)
{
DListNode<T> *newNode = new DListNode(newobj, trailer.prev,&trailer);
trailer.prev->next = newNode;
trailer.prev = newNode;
}
// remove the first object of the list
template <typename T>
T DoublyLinkedList<T>::removeFirst()
{ 
if (isEmpty())
throw EmptyDLinkedListException("Empty Doubly Linked List");
DListNode<T> *node = header.next;
node->next->prev = &header;
header.next = node->next;
T obj = node->obj;
delete node;
return obj;
}
// remove the last object of the list
template <typename T>
T DoublyLinkedList<T>::removeLast()
{
if (isEmpty())
throw EmptyDLinkedListException("Empty Doubly Linked List");
DListNode<T> *node = trailer.prev;
node->prev->next = &trailer;
trailer.prev = node->prev;
T obj = node->obj;
delete node;
return obj;
}
// destructor
template <typename T>
DoublyLinkedList<T>::~DoublyLinkedList()
{
DListNode<T> *prev_node, *node = header.next;
while (node != &trailer) {
prev_node = node;
node = node->next;
delete prev_node;
}
header.next = &trailer;
trailer.prev = &header;
}
// return the first object
template <typename T>
T DoublyLinkedList<T>::first() const
{ 
if (isEmpty())
throw EmptyDLinkedListException("Empty Doubly Linked List");
return header.next->obj;
}
// return the last object
template <typename T>
T DoublyLinkedList<T>::last() const
{
if (isEmpty())
throw EmptyDLinkedListException("Empty Doubly Linked List");
return trailer.prev->obj;
}
// return the list length
template <typename T>
T DoublyLinkedListLength(DoublyLinkedList<T>& dll) {
DListNode<T> *current = dll.getFirst();
int count = 0;
while(current != dll.getAfterLast()) {
count++;
current = current->getNext(); //iterate
}
return count;
}
// output operator
template <typename T>
ostream& operator<<(ostream& out, const DoublyLinkedList<T>& dll) {
DListNode<T> *current = dll.getFirst();
while (current != dll.getAfterLast()) {
out << current->getElem() << 'n';
current = current->getNext();
}
return out;
}

TemplateMain.cpp

#include "TemplateDoublyLinkedList.h"
#include <iostream>
#include <string>
#include <cstdio>
#include <sstream>
using namespace std;
int main () {
// Construct a linked list with header & trailer
cout << "Create a new list" << endl;
DoublyLinkedList<string> dll;
cout << "list: " << dll << endl << endl;
// Insert 10 nodes at back with value 10,20,30,..,100
cout << "Insert 10 nodes at back with value 10,20,30,..,100" << endl;
for (int i=10;i<=100;i+=10) {
stringstream ss;
ss << i;
dll.insertLast(ss.str());
}
cout << "list: " << dll << endl << endl;
// Insert 10 nodes at front with value 10,20,30,..,100
cout << "Insert 10 nodes at front with value 10,20,30,..,100" << endl;
for (int i=10;i<=100;i+=10) {
stringstream ss;
ss << i;
dll.insertFirst(ss.str());
}
cout << "list: " << dll << endl << endl;
// Copy to a new list
cout << "Copy to a new list" << endl;
DoublyLinkedList<string> dll2(dll);
cout << "list2: " << dll2 << endl << endl;
// Assign to another new list
cout << "Assign to another new list" << endl;
DoublyLinkedList<string> dll3=dll;
cout << "list3: " << dll3 << endl << endl;
// Delete the last 10 nodes
cout << "Delete the last 10 nodes" << endl;
for (int i=0;i<10;i++) {
dll.removeLast();
}
cout << "list: " << dll << endl << endl;
// Delete the first 10 nodes
cout << "Delete the first 10 nodes" << endl;
for (int i=0;i<10;i++) {
dll.removeFirst();
}
cout << "list: " << dll << endl << endl;
// Check the other two lists
cout << "Make sure the other two lists are not affected." << endl;
cout << "list2: " << dll2 << endl;
cout << "list3: " << dll3 << endl;
return 0;
}

现在它列出了一堆错误,但所有错误中有一个共同的主题是(总结):"在编译类模板成员函数'DoublyLinkedList::DoublyLinkedList(const DoublyLink edList&)'时,T=std::string">

所以我认为这意味着我的模板有一个错误,它不接受字符串进入模板,但不知道如何修复它。谢谢你的帮助。

编辑:我想我修复了所有的位置,比如DListNode*current=dll.getFirst();

但我仍然收到一些错误,比如在第24行,它说使用类模板需要模板参数列表

我没有查看所有的类定义,但即使是DoublyLinkedList的第一个构造函数也是无效的

DoublyLinkedList() : header(0), trailer(0) // constructor
{ header.next = &trailer; trailer.prev = &header; }

如果标题和尾部由零初始化,则两个分配网

header.next = &trailer; trailer.prev = &header;

可能导致程序异常终止。

此外,他们没有任何意义。构造函数应该很简单,没有这些赋值

DoublyLinkedList() : header(0), trailer(0) {}

此外,您的定义还有其他错误。例如,代替

DListNode* current = dll.getFirst();

应该有

DListNode<T>* current = dll.getFirst();

因为类DListNode是一个模板类。

此外,这也是一个糟糕的列表设计。类DListNode应该是DoublyLinkedList的内部类。将它与类DoublyLinkedList分开定义是没有意义的。此外,如果DoublyLinkedList是DListNode的朋友类,则不清楚为什么定义方法getNext和getPrevi。