passing linked lists to functions

P

Pushkar Pradhan

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
 
V

Victor Bazarov

Pushkar Pradhan said:
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
 
U

Unforgiven

Victor said:
'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.
 
R

Rolf Magnus

Pushkar said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top