为什么我的链表模板类在创建一个包含链表的类的对象时失败?
C++ Why does my Linked List template class fail when I create an object of a class that has a linked list in it?
我编写了这个LinkedList模板类,它还没有完成——我还需要添加安全特性和更多的方法。现在我需要它做什么它就做什么。但在某些情况下,它失败了,我不知道为什么。
template<class data_type> class LinkedList {
private:
struct Node {
data_type data;
Node* prev;
Node* next;
Node() : prev(NULL), next(NULL) {}
};
Node* head;
Node* GetLastNode() {
Node* cur = head;
while (cur->next != NULL)
cur = cur->next;
return cur;
}
public:
LinkedList() {
head = new Node;
head->prev = head;
head->next = NULL;
}
LinkedList(LinkedList<data_type> &to_copy) {
head = new Node;
head->prev = head;
head->next = NULL;
for (int i = 1; i <= to_copy.NumberOfItems(); i++) {
this->AddToList(to_copy.GetItem(i));
}
}
~LinkedList() {
DeleteAll();
delete head;
head = NULL;
}
void AddToList(const data_type data) {
Node* last = GetLastNode();
Node* newnode = last->next = new Node;
newnode->prev = last;
newnode->data = data;
}
void Delete(const unsigned int position) {
int currentnumberofitems = NumberOfItems();
Node* cur = head->next;
int pos = 1;
while (pos < position) {
cur = cur->next;
pos++;
}
cur->prev->next = cur->next;
if (position != currentnumberofitems)
cur->next->prev = cur->prev;
delete cur;
}
void DeleteAll() {
Node* last = GetLastNode();
Node* prev = last->prev;
while (prev != head) {
delete last;
last = prev;
prev = last->prev;
}
head->next = NULL;
}
data_type GetItem(unsigned int item_number) {
Node* cur = head->next;
for (int i = 1; i < item_number; i++) {
cur = cur->next;
}
return cur->data;
}
data_type* GetItemRef(unsigned int item_number) {
Node* cur = head->next;
for (int i = 1; i < item_number; i++) {
cur = cur->next;
}
return &(cur->data);
}
int NumberOfItems() {
int count(0);
Node* cur = head;
while (cur->next != NULL) {
cur = cur->next;
count++;
}
return count;
}
};
我在问题中陈述了我的问题,下面是一个例子:
class theclass {
public:
LinkedList<int> listinclass;
};
void main() {
LinkedList<theclass> listoftheclass;
theclass oneclass;
oneclass.listinclass.AddToList(5);
listoftheclass.AddToList(oneclass);
cout << listoftheclass.GetItem(1).listinclass.GetItem(1);
}
我不明白为什么它不能正常运行。
您需要实现一个赋值操作符。问题从这个函数开始:
void AddToList(const data_type data) {
Node* last = GetLastNode();
Node* newnode = last->next = new Node;
newnode->prev = last;
newnode->data = data; <---------------------------- Right there
}
因为data_type是你的类,而你没有一个合适的赋值操作符,你只是得到一个成员一个成员的(浅)复制。
参见"三原则"
你可能还应该实现一个交换函数,并让你的赋值操作符使用它。
参见复制和交换习语
在c++ 03中,局部类不能作为模板参数。将theclass
移到main
之外,它将工作。
相关文章:
- 链表,将列表复制到另一个列表
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 如何在 c++ 中比较 2 个链表并将匹配的数据放入另一个链表中
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 仅显示链表的最后一个元素
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 创建了一个链表,但如何删除 c++ 中的"所有"节点
- 在C++的链表末尾插入一个节点
- 在解决链表问题时创建一个额外的节点是一个好习惯吗?
- 插入一个基本的单向链表节点似乎破坏了我的 c++ 代码?
- C++:如何做一个链表,结构作为参数传递?
- 程序的输出不是来的,它是一个链表程序
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 链表指针赋值为什么我们不能直接将尾巴分配给 temp 而不是尾巴>尾巴下一个
- 你能在链表中有一个动态数组和它的内存分配吗
- 如何编写一个类似于kernellist_head的c++风格的双链表实现
- 为什么T是未定义的?我正在尝试实现一个用于双链表的节点类,它不喜欢我使用友元运算符后的T
- C++链表最后一个元素的迭代器?
- C++ 链表程序打印最后一个数据项 n 次
- 在 "CodePad" 中执行链表操作时转储的核心(这是一个在线C++编译器)