![]() |
| ||
| Passing Linked Lists - Syntax 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] static void Test1() 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. class list |
| ||
| Re: Passing Linked Lists - Syntax Hmm quick suggestion probably a crazy one :D does this work static void Test2(list &tester) |
| ||
| Re: Passing Linked Lists - Syntax 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++? |
| ||
| Re: Passing Linked Lists - Syntax 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. |
| ||
| Re: Passing Linked Lists - Syntax you need to add the & on the function declaration and definition Chris |
| ||
| Re: Passing Linked Lists - Syntax Ah, that's it. Thanks very much for the help. |
| All times are GMT -4. The time now is 4:47 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC