943,584 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2810
  • C++ RSS
Oct 21st, 2008
0

Linked List Class - Operator Overloading

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Oct 21st, 2008
0

Re: Linked List Class - Operator Overloading

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
n4naeem is offline Offline
8 posts
since Oct 2008
Oct 21st, 2008
0

Re: Linked List Class - Operator Overloading

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
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Oct 21st, 2008
0

Re: Linked List Class - Operator Overloading

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 ...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
n4naeem is offline Offline
8 posts
since Oct 2008
Oct 21st, 2008
0

Re: Linked List Class - Operator Overloading

Here is my function AddRecord, as you asked

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Oct 21st, 2008
0

Re: Linked List Class - Operator Overloading

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

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: calculator and loop problem
Next Thread in C++ Forum Timeline: Creating a character





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC