Question

I am working on overloaded operators(I love it!). While carrying out tests in Main, I got a segmentation fault.I think the problem here is on line 63(operator=() function) in List.cc. And it is affected when I typed list=list+list3 on line 34 in Main.cc and ran it. I am trying to avoid memory leaks that is why I'm using "delete". Does anyone know how to stop segmentation faults without creating memory leaks in this program?

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;
}
Was it helpful?

Solution

I believe that your problem originates in the List::operator+ function.

First of all, it should generally be declared and implemented as a global function.

Second, regardless of that, it cannot return a reference to an object created in the stack.

You need to return the object by value, and so you also need to add a copy-constructor in class List.

Here is how you should implement this function:

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

You should also return a reference to the calling object at the end of the += operator:

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

And I am leaving the copy-constructor for you to implement:

List::List(const List& list)
{
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top