c++ - 段错误

标签 c++ memory-leaks segmentation-fault operator-overloading

我正在研究重载运算符(我喜欢它!)。在 Main 中进行测试时,我遇到了段错误。我认为问题出在 List.cc 中的第 63 行(operator=() 函数)。当我在 Main.cc 的第 34 行键入 list=list+list3 并运行它时,它会受到影响。我试图避免内存泄漏,这就是我使用“删除”的原因。有谁知道如何在不在此程序中造成内存泄漏的情况下停止段错误?

列表.cc

#include <iostream>
using namespace std;

#include "List.h"

List::List() : head(0) { }

List::~List() 
{ 
  Node* currNode = head;
  Node* nextNode;

  while (currNode != 0) {
    nextNode = currNode->next;
    delete currNode;
    currNode = nextNode;
  }
}

void List::addFront(Student* stu) 
{ 
  Node* newNode = new Node(stu);

  newNode->next = head;
  head = newNode;

}

void List::addBack(Student* stu) 
{
  Node* newNode = new Node(stu);
  Node *currNode, *prevNode;

  prevNode = 0;
  currNode = head;

  while (currNode != 0) {
    prevNode = currNode;
    currNode = currNode->next;
  }
  if (head == 0)
    head = newNode;
  else
    prevNode->next = newNode;
}

void List::print() const
{
  Node* currNode = head;

  while (currNode != 0) {
    currNode->data->print();
    currNode = currNode->next;
  }
}

List& List::operator=(List& list){
  Node* thisNode= head;
  Node* currNode= list.head;
  Node* prev;
  while (thisNode != 0) {
    prev = thisNode->next;
    delete thisNode;------------------------------------------->line 63
    thisNode = prev;
  }
  head=0;
  *this+=list;

  return *this;

}

void List::operator+=(Student* data){
  addBack(data);
}

void List::operator+=(List& list){
  Node* currNode= list.head;

  while (currNode != 0) {
    addBack(currNode->data);
    currNode = currNode->next;
  }
}

void List::operator+(Student* data){

}

List& List::operator+(List& list){
  List newList;
  newList+=*this;
  newList+=list;

  List& newL=newList;
  return newList;
}


List::Node::Node(Student* stu) : data(stu), next(0) { }

主.cc

#include <iostream>
using namespace std;

#include "List.h"
#include "Student.h"

int main()
{

  Student matilda("100567899", "Matilda");
  Student joe("100234555", "Joe");
  Student timmy("100234888", "Timmy");
  Student john("100503954", "John");

  List comp2404;
  List list, list2, list3;

  comp2404.addBack(&matilda);
  comp2404.addBack(&joe);
  comp2404.addBack(&timmy);

  list2.addBack(&matilda);
  list2.addBack(&joe);
  list2.addBack(&timmy);

  list2=list3=list=comp2404;

  comp2404.print();
  list.print();
  cout<<""<<endl;
  list3+=&john;
  list3=list;
  //list+=list3;
  list=list+list3;----------------------------------------line 34
  cout<<"list"<<endl;
  list.print();
  cout<<"list2"<<endl;
  list2.print();
  cout<<"list3"<<endl;
  list3.print();

  return 0;
}

最佳答案

我认为您的问题源于 List::operator+ 函数。

首先,一般应该作为一个全局函数来声明和实现。

其次,无论如何,它都不能返回对在堆栈中创建的对象的引用。

您需要按值返回对象,因此您还需要在 List 类中添加一个复制构造函数。

下面是你应该如何实现这个功能:

List operator+(const List& list1,const List& list2)
{
    List newList;
    newList+=list1;
    newList+=list2;
    return newList;
}

您还应该在 += 运算符的末尾返回对调用对象的引用:

List& List::operator+=(const List& list)
{
    ...
    return *this;
}

我将复制构造函数留给你去实现:

List::List(const List& list)
{
    ...
}

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

相关文章:

C++ 预处理器标准行为

c# - 如何解决私有(private)字节( native 内存)泄漏?

c++ - boost 信号量导致段错误

c++ - OpenCV 功能检测和匹配 - 绘图匹配段错误

c++ - 无法访问指针 vector map 元素,

c++ - 为 arm-none-eabi-gcc 交叉编译库

ios - 如何调查 ios 中的内存泄漏?

c++ - 使用旧语法的 .NET C++ 内存泄漏?

c - 段错误 : Cannot access memory at address

c++ - 如何避免长 switch 语句? C++