Passing Linked Lists - Syntax

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

Join Date: Oct 2008
Posts: 11
Reputation: teddybouch is an unknown quantity at this point 
Solved Threads: 0
teddybouch teddybouch is offline Offline
Newbie Poster

Passing Linked Lists - Syntax

 
0
  #1
Dec 4th, 2008
I'm having trouble with passing a linked list between functions. I did some research online and found a few things, one of which was here, that led to the conclusion that I am not passing my linked list by reference. However, I have tried numerous different syntaxes to do this, and nothing seems to work. If I add an ampersand to the list I'm trying to pass, I get this error:

"error: invalid initialization of non-const reference of type 'list&' from a temporary of type 'list*'"

Below is a vastly simplified and shorter piece of code that replicates the problem. I can confirm that the program crashes with a segfault when the call to Test2 is made, but not if it is commented out. In addition, after the call to Test2, the contents of tester are printed as After Test2, tester = [0.000000, 0.000000; 0.000000, 0.000000]
  1. static void Test1()
  2. {
  3. // Variable declarations
  4. list tester;
  5. double tempArray[2];
  6.  
  7. // Create a linked list to test with
  8. for (int i = 0; i<10; i++)
  9. {
  10. tester.append(i, 2*i);
  11. }
  12.  
  13. // Print the list for test output
  14. printf("After creation, tester = [");
  15. for (int i = 0; i<(tester.count()-1); i++)
  16. {
  17. tempArray[0] = tester.getx(i);
  18. tempArray[1] = tester.gety(i);
  19. printf("%f, %f; ", tempArray[0], tempArray[1]);
  20. }
  21. tempArray[0] = tester.getx(tester.count()-1);
  22. tempArray[1] = tester.gety(tester.count()-1);
  23. printf("%f, %f]\n", tempArray[0], tempArray[1]);
  24.  
  25. // Pass the list to another function, hopefully by reference
  26. //Test2(tester);
  27.  
  28. // Print the list again for comparison
  29. printf("After Test2, tester = [");
  30. for (int i = 0; i<(tester.count()-1); i++)
  31. {
  32. tempArray[0] = tester.getx(i);
  33. tempArray[1] = tester.gety(i);
  34. printf("%f, %f; ", tempArray[0], tempArray[1]);
  35. }
  36. tempArray[0] = tester.getx(tester.count()-1);
  37. tempArray[1] = tester.gety(tester.count()-1);
  38. printf("%f, %f]\n", tempArray[0], tempArray[1]);
  39.  
  40. }
  41.  
  42. static void Test2(list tester)
  43. {
  44. // Variable declarations
  45. double x, y;
  46. int int_x;
  47.  
  48. // Go through the list, and remove every element for which the first term
  49. // in the array is even
  50. for (int i = 0; i<(tester.count()-1); i++)
  51. {
  52. x = tester.getx(i);
  53. y = tester.gety(i);
  54.  
  55. int_x = (static_cast<int> (x));
  56.  
  57. if ((int_x%2)==0)
  58. {
  59. tester.del(x, y);
  60. }
  61. }
  62. }

In addition, here's the code from the linked list class that I wrote, in case that's where the problem is. However, I stole most of it from online, so I doubt that's the case.

  1. class list
  2. {
  3. private:
  4.  
  5. struct node
  6. {
  7. double xPoint;
  8. double yPoint;
  9. node *link;
  10. }*p;
  11.  
  12. public:
  13.  
  14. list();
  15. void append( double xNew, double yNew );
  16. void del( double xDel, double yDel );
  17. double getx( int index );
  18. double gety( int index );
  19. int count();
  20. ~list();
  21. };
  22.  
  23. list::list()
  24. {
  25. p=NULL;
  26. }
  27.  
  28. void list::append(double xNew, double yNew)
  29. {
  30. node *q,*t;
  31.  
  32. if( p == NULL )
  33. {
  34. p = new node;
  35. p->xPoint = xNew;
  36. p->yPoint = yNew;
  37. p->link = NULL;
  38. }
  39. else
  40. {
  41. q = p;
  42. while( q->link != NULL )
  43. q = q->link;
  44. t = new node;
  45. t->xPoint = xNew;
  46. t->yPoint = yNew;
  47. t->link = NULL;
  48. q->link = t;
  49. }
  50. }
  51.  
  52. void list::del( double xDel, double yDel )
  53. {
  54. node *q,*r;
  55. q = p;
  56. if(( q->xPoint == xDel ) && ( q->yPoint == yDel ))
  57. {
  58. p = q->link;
  59. delete q;
  60. return;
  61. }
  62.  
  63. r = q;
  64. while( q!=NULL )
  65. {
  66. if(( q->xPoint == xDel ) && ( q->yPoint == yDel ))
  67. {
  68. r->link = q->link;
  69. delete q;
  70. return;
  71. }
  72.  
  73. r = q;
  74. q = q->link;
  75. }
  76. }
  77.  
  78. double list::getx( int index )
  79. {
  80. double ans;
  81. int count = 0;
  82.  
  83. node *q;
  84. q = p;
  85.  
  86. while( count < index )
  87. {
  88. q = q->link;
  89. count++;
  90. }
  91.  
  92. ans = q->xPoint;
  93.  
  94. return ans;
  95. }
  96.  
  97. double list::gety( int index )
  98. {
  99. double ans;
  100. int count = 0;
  101.  
  102. node *q;
  103. q = p;
  104.  
  105. while( count < index )
  106. {
  107. q = q->link;
  108. count++;
  109. }
  110.  
  111. ans = q->yPoint;
  112.  
  113. return ans;
  114. }
  115.  
  116. int list::count()
  117. {
  118. node *q;
  119. int c=0;
  120. for( q=p ; q != NULL ; q = q->link )
  121. c++;
  122.  
  123. return c;
  124. }
  125.  
  126. list::~list()
  127. {
  128. node *q;
  129. if( p == NULL )
  130. return;
  131.  
  132. while( p != NULL )
  133. {
  134. q = p->link;
  135. delete p;
  136. p = q;
  137. }
  138. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Passing Linked Lists - Syntax

 
0
  #2
Dec 4th, 2008
Hmm quick suggestion probably a crazy one does this work

static void Test2(list &tester)
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: teddybouch is an unknown quantity at this point 
Solved Threads: 0
teddybouch teddybouch is offline Offline
Newbie Poster

Re: Passing Linked Lists - Syntax

 
0
  #3
Dec 4th, 2008
Oh, okay. Yes, that worked. Could someone possibly explain to me why? I thought that the declaration had to be the same as the line at the top of the function, but this apparently works with the ampersand just on the latter and without one t the function call. Is this unique to pointers, or am I forgetting something about C++?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: teddybouch is an unknown quantity at this point 
Solved Threads: 0
teddybouch teddybouch is offline Offline
Newbie Poster

Re: Passing Linked Lists - Syntax

 
0
  #4
Dec 4th, 2008
Sorry, I spoke too soon. I had a key line commented out while I was trying something myself. Your suggestion just changed the error message, so now I get:

error: 'void Test2(list)' used but never defined

Interestingly, the line number that is called out is the function declaration line at the top.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Passing Linked Lists - Syntax

 
0
  #5
Dec 4th, 2008
you need to add the & on the function declaration and definition

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: teddybouch is an unknown quantity at this point 
Solved Threads: 0
teddybouch teddybouch is offline Offline
Newbie Poster

Re: Passing Linked Lists - Syntax

 
0
  #6
Dec 4th, 2008
Ah, that's it. Thanks very much for the help.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC