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