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

发布于 2024-11-24 03:20:52 字数 2528 浏览 1 评论 0原文

我编写了这个 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);
}

我无法弄清楚为什么它运行不正确。

I wrote this LinkedList template class, which isn't finished yet- I have yet to add safety features and more methods. As of now it does what I need it to. But it fails in a certain situation and I don't know why.

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;
}
};

I stated my problem in the question and here is an example:

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);
}

I can't figure out why it doesn't run right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放我走吧 2024-12-01 03:20:52

您需要实现一个赋值运算符。问题始于此函数:

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 是您的类,并且您没有适当的赋值运算符,因此您只是在那里逐个成员(浅)副本获取成员。

请参阅三法则

您还应该实现一个交换功能,并且有你的赋值运算符使用它。

请参阅复制和交换惯用语

You need to implement an assignment operator. The problem starts in this function here:

void AddToList(const data_type data) {
    Node* last = GetLastNode();
    Node* newnode = last->next = new Node;
    newnode->prev = last;
    newnode->data = data; <---------------------------- Right there
}

Since data_type is your class, and you don't have an appropriate assignment operator, you are just getting a member by member(shallow) copy there.

See The Rule of Three

You should also probably implement a swap function, and have your assignment operator use that.

See Copy and Swap Idiom

风追烟花雨 2024-12-01 03:20:52

在 C++03 中,局部类不能作为模板参数。将 theclass 移到 main 之外,它将起作用。

在 C++0x 中,这个限制被删除。

In C++03, local classes can't be template arguments. Move theclass outside of main, and it will work.

In C++0x this limitation is removed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文