c++ - 链表中的唯一指针 - 未处理的异常,堆栈溢出

标签 c++ pointers unique-ptr

首先,我要感谢您在过去几个小时内为我提供的所有帮助。我一直在努力解决这个问题,如何从原始指针转换为唯一指针并让自己陷入很多错误。然而,在这个社区的帮助下,我很庆幸我的程序最终编译无误。但我想我还没到那儿。我觉得我离终点线只有一分钟的路程,所以我不会放弃,直到我解决它。我的程序一运行就崩溃,它说堆栈溢出并抛出异常。我想这一定是我在构造函数中将唯一指针声明和初始化为类成员的方式根本不正确,因此它从调用构造函数的那一刻起就崩溃了。谁能告诉我应该怎么做才能解决这个错误?谢谢。

这是我的主要cpp文件:

#include"ContactList.h"
#include<memory>

using namespace std;

int main()
{
    //ContactList* cl1 = new ContactList();

    unique_ptr<ContactList> cl1(new ContactList());
    string name;

    while(true)
    {
        cout << "Enter a name or q to quit: " << endl;
        cin >> name;
        if(name == "q")
            break;
        cl1->addToHead(name);
    }

    cl1->PrintList();
    return 0;
}

联系人列表.h

#pragma once
#include"Contact.h"
#include<memory>

using namespace std;

class ContactList
{
public:
    ContactList();
    void addToHead(const std::string&);
    void PrintList();

private:
    //Contact* head;
    unique_ptr<Contact> head;
    int size;
};

联系人列表.cpp

#include"ContactList.h"
#include<memory>

using namespace std;

ContactList::ContactList(): head(new Contact()), size(0)
{
}

void ContactList::addToHead(const string& name)
{
    //Contact* newOne = new Contact(name);
    unique_ptr<Contact> newOne(new Contact(name));

    if(head == 0)
    {
        head.swap(newOne);
        //head = move(newOne);
    }
    else
    {
        newOne->next.swap(head);
        head.swap(newOne);
        //newOne->next = move(head);
        //head = move(newOne);
    }
    size++;
}

void ContactList::PrintList()
{
    //Contact* tp = head;
    unique_ptr<Contact> tp(new Contact());
    tp.swap(head);
    //tp = move(head);

    while(tp != 0)
    {
        cout << *tp << endl;
        tp.swap(tp->next);
        //tp = move(tp->next);
    }
}

联系人.h

#pragma once
#include<iostream>
#include<string>
#include<memory>

class Contact
{
    friend std::ostream& operator<<(std::ostream& os, const Contact& c);
    friend class ContactList;

public:
    Contact(std::string name = "none");

private:
    std::string name;
    //Contact* next;    
    std::unique_ptr<Contact> next;
};

联系人.cpp
#include"Contact.h"

using namespace std;

Contact::Contact(string n):name(n), next(new Contact())
{
}

ostream& operator<<(ostream& os, const Contact& c)
{
    return os << "Name: " << c.name;
}

这是我得到的错误:

Unhandled exception at 0x77E3DEFE (ntdll.dll) in Practice.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x002B2F58).

最佳答案

您没有发布 Contact 的代码,但我认为它与您之前的一个问题中的代码相同:

Contact::Contact(string n):name(n), next(new Contact())
{
}

如您所见,构造一个Contact需要将它的next成员设置为一个新的Contact
为了构造那个联系人,您要为它的创建一个新的联系人 next 成员。
依此类推,直至无穷远。

这就是堆栈溢出的原因 - Contact 构造永远不会结束。

您可能不希望 next 成为新构造的 Contact 的任何内容,所以尝试

Contact::Contact(string n):name(n), next(0)
{
}

关于c++ - 链表中的唯一指针 - 未处理的异常,堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19221236/

相关文章:

c - fork 和共享内存

c++ - 在 C++ 中的 unique_ptr 之间进行转换

c++ - float 与定点数 : what are the pros/cons?

c++ - 带有迭代器和自定义步长的循环结束条件

python - 将 pybind11 绑定(bind)标记为已弃用的最佳方法

c++ - 混合 boost::optional 和 std::unique_ptr

c++ - unique_ptr 的动态转换

带模板的 C++ 函数指针

c - 这个指针实际上指向哪里?

C 程序 - 指针数组乘法