By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

passing linked lists to functions

P: n/a
I need to pass the STL linked list to a function, this function should
modify the linked list. So do I need to pass it by address, this is how
I do it:
void qhull(PARTICLE S[], int len, list<PARTICLE> &hull,
list<PARTICLE>::iterator
&iter1,
list<PARTICLE>::iterator &iter2, PARTICLE a, PARTICLE b,
int rank, int numtasks)

I think that's working but I have another problem: I need to pass two
iterators also to this function which point to certain position in the
list, should these iterators be passed by reference. The above syntax
gives me syntax errors:
This is the call from main:
qhull(S1, index1, hull, hull.begin(), hull.end(), globalMin,
globalMax, rank,
numtasks);

Pushkar Pradhan

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote...
I need to pass the STL linked list to a function, this function should
modify the linked list. So do I need to pass it by address, this is how
I do it:
void qhull(PARTICLE S[], int len, list<PARTICLE> &hull,
list<PARTICLE>::iterator
&iter1,
list<PARTICLE>::iterator &iter2, PARTICLE a, PARTICLE b,
int rank, int numtasks)

I think that's working but I have another problem: I need to pass two
iterators also to this function which point to certain position in the
list, should these iterators be passed by reference. The above syntax
gives me syntax errors:
This is the call from main:
qhull(S1, index1, hull, hull.begin(), hull.end(), globalMin,
globalMax, rank,
numtasks);


'begin' and 'end' members return a _temporary_ iterator, which cannot
be passed where a _non_const_ reference is expected. Change your 'qhull'
function to accept " list<PARTICLE>::iterator const& " for both 'iter1'
and 'iter2' arguments, and it will work.

Victor
Jul 22 '05 #2

P: n/a
Pushkar Pradhan wrote:
I need to pass the STL linked list to a function, this function should
modify the linked list. So do I need to pass it by address, this is
how I do it:
void qhull(PARTICLE S[], int len, list<PARTICLE> &hull,
list<PARTICLE>::iterator
&iter1,
list<PARTICLE>::iterator &iter2, PARTICLE a, PARTICLE b,
int rank, int numtasks)
You're not passing it by address, but rather by reference.
I think that's working but I have another problem: I need to pass two
iterators also to this function which point to certain position in the
list, should these iterators be passed by reference.
Are you sure you need both the list and the iterators? Anyway, I'd pass
them by value, since you probably have to copy them anyway.
The above syntax
gives me syntax errors:
This is the call from main:
qhull(S1, index1, hull, hull.begin(), hull.end(), globalMin,
globalMax, rank,
numtasks);


That's because hull.begin() and hull.end() return temporaries, and
you're not allowed to bind those to non-const references.

Jul 22 '05 #3

P: n/a
Victor Bazarov wrote:
I think that's working but I have another problem: I need to pass two
iterators also to this function which point to certain position in
the list, should these iterators be passed by reference. The above
syntax gives me syntax errors:
This is the call from main:
qhull(S1, index1, hull, hull.begin(), hull.end(), globalMin,
globalMax, rank,
numtasks);


'begin' and 'end' members return a _temporary_ iterator, which cannot
be passed where a _non_const_ reference is expected. Change your
'qhull' function to accept " list<PARTICLE>::iterator const& " for
both 'iter1' and 'iter2' arguments, and it will work.


That doesn't sound like a good idea to me, because you wouldn't be able to
call any non-const members, and I suppose (especially since his example call
uses begin() and end()) that he's going to want to use operator++, which is
non-const. Just pass the iterators by value, that should do the trick.

--
Unforgiven

"You can't rightfully be a scientist if you mind people thinking
you're a fool."
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.