Linked List Class - Operator Overloading

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Linked List Class - Operator Overloading

 
0
  #1
Oct 21st, 2008
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:

  1. template <class D>
  2. class SLList {
  3. SLListNode<D> *Head;
  4. SLListNode<D> *Tail;
  5. public:
  6. friend class BSTree<D>;
  7. static int Count;
  8. string Name;
  9. SLList(string id);
  10. ~SLList(void);
  11. int GetCount(void);
  12. D GetRecord(void);
  13. bool AddRecord(D record);
  14. void Show(void);
  15. bool RemoveRecord(void);
  16. bool InputRecord(int num, bool sort);
  17. SLList<D> operator+(SLList<D> ob2);
  18. };
  19.  
  20. template <class D>
  21. SLList<D> SLList<D>::operator+(SLList<D> ob2) {
  22. return *this;
  23. }
  24.  
  25. template <class D>
  26. SLList<D>::SLList(string id) {
  27. Name = id;
  28. Head = new SLListNode<D>;
  29. Tail = Head;
  30. Count++;
  31. return;
  32. }
  33.  
  34. template <class D>
  35. SLList<D>::~SLList(void) {
  36. SLListNode<D> *aux;
  37. aux = Head;
  38. while(aux!=Tail) {
  39. Head = Head->Next;
  40. delete aux;
  41. aux = Head;
  42. }
  43. delete aux;
  44. Count--;
  45. return;
  46. }
  47.  
  48. int main(void) {
  49. SLList<int> list1("IntList1");
  50. SLList<int> list2("IntList2");
  51. list1.InputRecord(0, true);
  52. list1.Show();
  53. list2.InputRecord(0, true);
  54. list2.Show();
  55. list1 = list1 + list2;
  56. cout << endl << "// here the program evaluates 'list1 = list1 + list2;' " << endl;
  57. // from here on both list1 and list2 are all messed up
  58. list1.Show();
  59. list2.Show();
  60. cin.get();
  61. return EXIT_SUCCESS;
  62. }

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:

  1. How many records would you like to add to the SLList 'IntList1'?
  2. 3
  3.  
  4. Please insert record [1]
  5. 1
  6.  
  7. Please insert record [2]
  8. 2
  9.  
  10. Please insert record [3]
  11. 3
  12.  
  13. The SLList 'IntList1' is:
  14. { 1, 2, 3 }
  15.  
  16. How many records would you like to add to the SLList 'IntList2'?
  17. 3
  18.  
  19. Please insert record [1]
  20. 4
  21.  
  22. Please insert record [2]
  23. 5
  24.  
  25. Please insert record [3]
  26. 6
  27.  
  28. The SLList 'IntList2' is:
  29. { 4, 5, 6 }
  30.  
  31. // here the program evaluates 'list1 = list1 + list2;'
  32.  
  33. The SLList 'IntList1' is:
  34. { 134525024, 134524960, 134525040 }
  35.  
  36. The SLList 'IntList2' is:
  37. { 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: n4naeem is an unknown quantity at this point 
Solved Threads: 1
n4naeem n4naeem is offline Offline
Newbie Poster

Re: Linked List Class - Operator Overloading

 
0
  #2
Oct 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Linked List Class - Operator Overloading

 
0
  #3
Oct 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: n4naeem is an unknown quantity at this point 
Solved Threads: 1
n4naeem n4naeem is offline Offline
Newbie Poster

Re: Linked List Class - Operator Overloading

 
0
  #4
Oct 21st, 2008
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 ...
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Linked List Class - Operator Overloading

 
0
  #5
Oct 21st, 2008
Here is my function AddRecord, as you asked

  1. template <class D>
  2. bool SLList<D>::AddRecord(D record) {
  3. Tail->Data = record;
  4. Tail->Next = new SLListNode<D>;
  5. Tail = Tail->Next;
  6. return true;
  7. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Linked List Class - Operator Overloading

 
0
  #6
Oct 21st, 2008
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

  1. template <class D>
  2. SLList<D>& SLList<D>::operator+(SLList<D> &ob2) {
  3. return *this;
  4. }

(note the &s) solves everything

Thank you for your time!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC