c++ - 链表段错误 11

标签 c++ linked-list

<分区>

我的一项作业有问题。我需要做一个链表连接,但是在重新定义 operator+ 之后,我有段错误。 这是代码:

在 main.cpp 中:

case CONCAT:
        do
    {
      std::cout << "Which two list do you want to concat?(1-3) ";
      std::cin >> s;
            std::cin >> g;
            selectedlist =atoi(s.c_str());
            selectedlist2 =atoi(g.c_str());
    } while ((
                 selectedlist== 0 && 
                 s != "0" && 
                 (selectedlist > 3 || selectedlist < 1)
             ) && 
             (
                  selectedlist2== 0 && 
                  g != "0" && 
                  (selectedlist2 > 3 || selectedlist2 < 1)
             )
            );
        Lista[selectedlist-1]+Lista[selectedlist2-1];
        std::cout<<"Ready";

   break;

在头文件中:

class lista
{
    public:
        enum Exceptions{EMPTY};
        lista() : first(NULL),last(NULL),current(NULL){ first = new Elem(0,0);}
        virtual ~lista();
        lista(const lista& s);
        int Current() const {return current->Adat;}
        void First()   {current = first->next;}
        bool End()     const {return current==NULL;}
        void Next()    {current = current->next;}
        void Vegere(int e);
        void Elejerol();
        bool Urese();
        int Eleje();
        friend std::ostream& operator<<(std::ostream& s, const lista& a);
        friend lista operator+(lista& a, lista& b);

    private:
        struct Elem{
                    int Adat;
                    Elem* next;
                    Elem(int c, Elem* n): Adat(c), next(n){};
                    };
        Elem* first;
        Elem* last;
        Elem* current;
};

在 lista.cpp 中:

lista operator+(lista& a, lista& b)
{
  if(b.first->next!=NULL && a.first->next!=NULL)
    {
        a.last->next = b.first->next;
        a.last = b.last;
        b.first = new lista::Elem(0,0);
        b.last = NULL;
        b.current = NULL;
    }
   else
   {
        throw lista::EMPTY;
   }
    return a;
}

void lista::Vegere(int e) { 
  Elem* p = new Elem(e,0); 
  if(last==NULL) { 
     first -> next = p; 
     last = p; 
  } 
  else { 
    last -> next = p; 
    last = p; 
  } 
} 

它提示,所有其他功能(清空、添加数字等)工作正常。我做错了什么?

void lista::Vegere(int e) 
{ 
    Elem* p = new Elem(e,0); 
    if(last==NULL) 
    { 
        first -> next = p; last = p; 
    } 
    else 
    { 
        last -> next = p; last = p; 
    } 
}

析构函数:

lista::~lista()
{
    Elem *p, *v;
    p = first;
    v = first -> next;
    while( v!=NULL)
    {
        delete p;
        p = v;
        v = v -> next;
    }
    delete p;
}

我想通了。 我没有在这里发布这部分代码,但经过一些调试后,我得到了它。

这里是错误:

    lista::lista(const lista& s){
        if(s.first->next==NULL)
        {
            first->next = last = NULL;
        }
        else
        {
        Elem* q = new Elem(s.first->Adat,NULL);
        first = q;
        for(Elem* p=s.first->next;p!=NULL;p=p->next)
        {
            q = new Elem(p->Adat,NULL);
            q->next = q;
        }
        last = q;
        }

        current = first;

THIS PART is the error:
[
        while(current!=NULL && current->Adat!=s.current->Adat)
        {
            current=current->next;
        }
]
    }

我刚刚删除了它,它可以正常工作。 :)

最佳答案

问题是

lista operator+(lista& a, lista& b)
{
    ...
    return a;
}

这会创建 a拷贝,然后立即将其销毁(因为您不会将其保存在任何地方)。您还没有定义复制构造函数,所以您让编译器定义了一个(它只是复制指针)。 temporary的析构函数会先删除a.first指向的内存,然后当你来调用a的析构函数时,一切就炸了。

如果您的析构函数正在释放内存,您需要声明一个复制构造函数和一个复制赋值运算符。最简单的此类定义是删除它们(尾随 = delete;)- 然后您需要更改 operator + 的定义(可能通过将函数名称更改为append()).

关于c++ - 链表段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40105890/

相关文章:

c++ - 异常规范作为函数声明中的注释

c++ - 在 C++ 中通过引用传递对象

代码出现段错误或从未完成其运行

c++ - C中函数执行的优先顺序

c++ - boost 或 Visual Studio 2010 中的内存泄漏

c++ - 添加选项以显示百分比时生成文件问题

c++:新的多维字符串数组

c - C 中的链表太慢

c - 链表的两种实现方式 : which is better?

c - 在C中的链表内添加元素到链表