| | |
Passing Linked Lists - Syntax
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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
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.
"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] C++ Syntax (Toggle Plain Text)
static void Test1() { // Variable declarations list tester; double tempArray[2]; // Create a linked list to test with for (int i = 0; i<10; i++) { tester.append(i, 2*i); } // Print the list for test output printf("After creation, tester = ["); for (int i = 0; i<(tester.count()-1); i++) { tempArray[0] = tester.getx(i); tempArray[1] = tester.gety(i); printf("%f, %f; ", tempArray[0], tempArray[1]); } tempArray[0] = tester.getx(tester.count()-1); tempArray[1] = tester.gety(tester.count()-1); printf("%f, %f]\n", tempArray[0], tempArray[1]); // Pass the list to another function, hopefully by reference //Test2(tester); // Print the list again for comparison printf("After Test2, tester = ["); for (int i = 0; i<(tester.count()-1); i++) { tempArray[0] = tester.getx(i); tempArray[1] = tester.gety(i); printf("%f, %f; ", tempArray[0], tempArray[1]); } tempArray[0] = tester.getx(tester.count()-1); tempArray[1] = tester.gety(tester.count()-1); printf("%f, %f]\n", tempArray[0], tempArray[1]); } static void Test2(list tester) { // Variable declarations double x, y; int int_x; // Go through the list, and remove every element for which the first term // in the array is even for (int i = 0; i<(tester.count()-1); i++) { x = tester.getx(i); y = tester.gety(i); int_x = (static_cast<int> (x)); if ((int_x%2)==0) { tester.del(x, y); } } }
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.
C++ Syntax (Toggle Plain Text)
class list { private: struct node { double xPoint; double yPoint; node *link; }*p; public: list(); void append( double xNew, double yNew ); void del( double xDel, double yDel ); double getx( int index ); double gety( int index ); int count(); ~list(); }; list::list() { p=NULL; } void list::append(double xNew, double yNew) { node *q,*t; if( p == NULL ) { p = new node; p->xPoint = xNew; p->yPoint = yNew; p->link = NULL; } else { q = p; while( q->link != NULL ) q = q->link; t = new node; t->xPoint = xNew; t->yPoint = yNew; t->link = NULL; q->link = t; } } void list::del( double xDel, double yDel ) { node *q,*r; q = p; if(( q->xPoint == xDel ) && ( q->yPoint == yDel )) { p = q->link; delete q; return; } r = q; while( q!=NULL ) { if(( q->xPoint == xDel ) && ( q->yPoint == yDel )) { r->link = q->link; delete q; return; } r = q; q = q->link; } } double list::getx( int index ) { double ans; int count = 0; node *q; q = p; while( count < index ) { q = q->link; count++; } ans = q->xPoint; return ans; } double list::gety( int index ) { double ans; int count = 0; node *q; q = p; while( count < index ) { q = q->link; count++; } ans = q->yPoint; return ans; } int list::count() { node *q; int c=0; for( q=p ; q != NULL ; q = q->link ) c++; return c; } list::~list() { node *q; if( p == NULL ) return; while( p != NULL ) { q = p->link; delete p; p = q; } }
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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++?
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- High-Level Languages (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Simple Recursion (Multiplying by using Addition)
- Next Thread: Double variable type, unexpected answer
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





does this work