0

So I did this sorted doubly linked list code. I am supposed to overload a couple of operators. However, when I try to create a new SDL (sorted doubly list) inside one of the functions, it will destroy itself before I manage to return it to the main function. I tried to debug this so many times. I see where the error occurs but I have no clue how to solve it. Here is the code where things mess up.

SortedDoublyLinkedList SortedDoublyLinkedList::operator+(const SortedDoublyLinkedList &    otherList)
{
    SortedDoublyLinkedList myNewList(otherList);
    Node *currentNode = head;
    while (currentNode) {
        myNewList.add(currentNode->value);
        currentNode = currentNode->next;
    }
    return myNewList; // destroys itself
}

Above function will overload when in the main the coder writes "mylist = mylist + mylist2" to add 2 lists together.

Here is another:

SortedDoublyLinkedList SortedDoublyLinkedList::operator+(const int & myNumber)
{
    SortedDoublyLinkedList myNewList(*this);
    myNewList.add(myNumber);
    return myNewList;    // destroys itself
}

Same deal.

Test cases are these below:

myList += myList2; // works properly!

myList += myList + 15; // works properly!

SortedDoublyLinkedList myList3 = myList + 15; // creates a new list without modifying myList, works properly!

Error occurs when the user types in:

myList = myList + myList2;

myList = myList + 15;

I got no clue why this is happening. Inside the above 2 functions, myNewList will destroy itself before returning, then at the very end of the program, it will try to destroy itself one more time (what?) but since it's already deallocated it will fail destroying it and then an error pops up.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    No, the object isn't destroyed before it's returned. What happens on `return` is that a temporary object is created, and the object you return is copied to the temporary object using the copy-constructor. *Then* the local object is destructed, and the temporary object is copied (once again using the copy-constructor) to the variable you assign the result of the function to, and the temporary object is destructed. There might be some optimizations done (like not having a temporary object) and copy elision, but the object you return will not be destructed until it has been copied. – Some programmer dude Aug 01 '14 at 11:15
  • 1
    You might want to check your copy-constructor to make sure it does the right thing (including freeing the nodes if the list is not empty), and also run in a debugger to catch any crashes, and maybe even step through the code line by line to see that everything works as intended. – Some programmer dude Aug 01 '14 at 11:17

2 Answers2

1

You have a copy-assignment operator only to assign single numbers, not to copy a whole list. If you want to assign a list to another list, you need a proper copy-assignment operator which does a deep copy.

When you do

myList = myList + myList2;

the compiler will translate it as

myList.operator=(myList + myList2);

You might also want to read about the rule of three.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Defining a copy-assignment operator solved it. I was looking in the wrong direction the entire time, thank you. –  Aug 01 '14 at 20:00
0

This is not the way you should write binary operator +.

See https://stackoverflow.com/a/4421719/3819284 at Binary arithmetic operators

Edit: as Joachim Pileborg said, your code lacks a proper assignment operator for SortedDoublyLinkedList.

Community
  • 1
  • 1
Gall
  • 1,595
  • 1
  • 14
  • 22
  • This is useful information, but it is not an answer. You should have posted it as a comment, instead. – Cody Gray - on strike Aug 01 '14 at 11:25
  • @CodyGray Well, I choose to add this as an answer because I think it might be part of the problem. I plan to edit with more info if I can reproduce the error or I'll delete my answer if this is not the case. – Gall Aug 01 '14 at 11:28