| | |
Linked List Class - Operator Overloading
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Hi all.
I am trying to overload the operator+ for a SLinkedList class I created. I would like to write
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:
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:
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
Anticipate thanks for every hint
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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

Anticipate thanks for every hint
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
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
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
•
•
Join Date: Oct 2008
Posts: 8
Reputation:
Solved Threads: 1
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 ...
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 ...
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Here is my function AddRecord, as you asked
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
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
Thanks for the effort
C++ Syntax (Toggle Plain Text)
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
Last edited by mrboolf; Oct 21st, 2008 at 10:05 am.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Nevermind, I solved it 
It was simple (as it always looks like AFTER
). 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
(note the &s) solves everything
Thank you for your time!

It was simple (as it always looks like AFTER
). 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 C++ Syntax (Toggle Plain Text)
template <class D> SLList<D>& SLList<D>::operator+(SLList<D> &ob2) { return *this; }
(note the &s) solves everything

Thank you for your time!
![]() |
Similar Threads
- problem about linked list. (C++)
- This Pointer / Copy Constructor (C++)
- Checking for duplicates in a orderedered linked list (C++)
Other Threads in the C++ Forum
- Previous Thread: calculator and loop problem
- Next Thread: Creating a character
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





