分段错误错误

时间:2014-03-21 06:00:00

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

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

List.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) { }

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

1 个答案:

答案 0 :(得分:1)

我相信您的问题源于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)
{
    ...
}