Hi all.
I am trying to overload the operator+ for a SLinkedList class I created. I would like to write list1 = list1 + list2; and obtaining that list2 is merged into list1.
I thought it was simple but it didn't worked properly. I tried to simplify the problem as much as I could (to isolate the causes) and this is what I've got:

template <class D>
class SLList {
	SLListNode<D> *Head; 
	SLListNode<D> *Tail; 
	public:
	friend class BSTree<D>;
	static int Count; 
	string Name; 
	SLList(string id); 
	~SLList(void); 
	int GetCount(void); 
	D GetRecord(void); 
	bool AddRecord(D record); 
	void Show(void); 
	bool RemoveRecord(void); 
	bool InputRecord(int num, bool sort); 
	SLList<D> operator+(SLList<D> ob2);
};

template <class D>
SLList<D> SLList<D>::operator+(SLList<D> ob2) {
	return *this;
}

template <class D>
SLList<D>::SLList(string id) {
	Name = id;
	Head = new SLListNode<D>;
	Tail = Head;
	Count++;
	return;
}

template <class D>
SLList<D>::~SLList(void) {
	SLListNode<D> *aux;
	aux = Head;
	while(aux!=Tail) {
		Head = Head->Next;
		delete aux;
		aux = Head;
	}
	delete aux;
	Count--;
	return;
}

int main(void) {
	SLList<int> list1("IntList1");
	SLList<int> list2("IntList2");
	list1.InputRecord(0, true);
	list1.Show();
	list2.InputRecord(0, true);
	list2.Show();
	list1 = list1 + list2;
        cout << endl << "// here the program evaluates 'list1 = list1 + list2;' " << endl;
       // from here on both list1 and list2 are all messed up
	list1.Show();
	list2.Show();
	cin.get();
	return EXIT_SUCCESS;
}

What I expected it to do was simply... nothing! I thought that the expression "list1 + list2" would have returned list1, so that "list1 = list1" shouldn't have changed anything... However both list1 and list2 are all messed up in the following .Show()s .
Here's and example output:

How many records would you like to add to the SLList 'IntList1'?
3

Please insert record [1]
1

Please insert record [2]
2

Please insert record [3]
3

The SLList 'IntList1' is:
{ 1, 2, 3 }

How many records would you like to add to the SLList 'IntList2'?
3

Please insert record [1]
4

Please insert record [2]
5

Please insert record [3]
6

The SLList 'IntList2' is:
{ 4, 5, 6 }

// here the program evaluates 'list1 = list1 + list2;'

The SLList 'IntList1' is:
{ 134525024, 134524960, 134525040 }

The SLList 'IntList2' is:
{ 134525072, 134525008, 134525088 }

Even when the operator+ overloading was effective it caused the same problem: it actually merged the two lists in the right way (I knew it for putting a Show() call from inside operator+) but after exiting the function both the list were all messed up. I think this is caused by some problem with making a copy of the object... should I overload the constructor? And if yes, any hint on "how" ?
I'm stuck :S
Anticipate thanks for every hint :)

template <class D>SLList<D> SLList<D>::operator+(SLList<D> ob2) { return *this;}

for this funtion u have to traverse all the lists and add them up !

this return status just returning the one list means which object invoke the + operator will be returned ...

make it clear please

Yes I know that this operator+ doesn't actually make sense. I wrote it this way to simplify my question ^^".

I wrote a version that worked (as far as merging the lists togheter) but there was a problem after exiting the function: both list1 and list2 appeared messed up.
So I wrote this "purposeless" operator+ to find out if the problem was in my merging routine or in the way it returned the object. It's still giving the same error, so I guess the problem is in the way it returns *this (couldn't be elsewhere, I think Oo).

I hope I made myself clear ^^"
Thanks again :)

there could be two type of problem here i can see !

First

template <class D>SLList<D> SLList<D>&::operator+(SLList<D> ob2) {
return this+obj2;
}

Return type should be Reference to this pointer.


Secondly.

i cant see urs add recode function.
add recode should add a Node with dynamic added memory.

try to figurout with these statements...

if you could can u share urs all code i will send u with working code.

looking forward to help u ...

Here is my function AddRecord, as you asked

template <class D>
bool SLList<D>::AddRecord(D record) {
	Tail->Data = record;
	Tail->Next = new SLListNode<D>;
	Tail = Tail->Next;
        return true;
}

However, I don't think the problem lies there... AddRecord worked perfectly so far and in this specific case is not even called!

I didn't posted all my functions because they are not involved with my actual question.
I just want to know why the line list1 = list1 + list2; doesn't work like list1 = list1; , given my "dumb" definition of operator+.
The first messes up both list1 and list2, and I can't see why.
The second obviously works fine.

Also, I don't understand why I should put return this+ob2; instead of return *this; at the end of my operator+. Isn't that recursively calling the function in an endless loop? Please clarify ^^"

Thanks for the effort :)

Nevermind, I solved it :)
It was simple (as it always looks like AFTER :P). When releasing the memory for the copy object, at the exit of the function, it released the original object's memory also. Writing it like this

template <class D>
SLList<D>& SLList<D>::operator+(SLList<D> &ob2) {
	return *this;
}

(note the &s) solves everything :)

Thank you for your time!

This question has already been answered. Start a new discussion instead.