C++ 当我创建一个包含链表的类的对象时,为什么我的链表模板类会失败?

标签 c++ oop templates linked-list dynamic-allocation

我写了这个 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 是您的类,并且您没有合适的赋值运算符,您只是在那里通过成员(浅)拷贝获取成员。

参见 The Rule of Three

您可能还应该实现一个交换函数,并让您的赋值运算符使用它。

参见 Copy and Swap Idiom

关于C++ 当我创建一个包含链表的类的对象时,为什么我的链表模板类会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6700576/

相关文章:

c++ - 使用数组时表达式必须具有类类型

c++ - 在函数签名中一起使用 * 和 &

Javascript OOP - 继承、原型(prototype)、回调函数

c++ - 将 C++ 类私有(private)变量转换为公共(public)变量

c++ - 基本数据类型变量 VS 对象变量

c++ - 为 std::to_string() 获取自己的补丁,以便在旧编译器上与 double 一起正常工作

c++编译错误在调试但不是在发布

javascript - 如何在指令模板中包含模板?

java - 通过另一个方法获取方法的局部变量

继承属性类的 c# OO 建议