passing linked lists to functions

Discussion in 'C++' started by Pushkar Pradhan, Nov 26, 2003.

  1. 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
    Pushkar Pradhan, Nov 26, 2003
    #1
    1. Advertising

  2. "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)
    >
    > 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
    Victor Bazarov, Nov 26, 2003
    #2
    1. Advertising

  3. Pushkar Pradhan

    Unforgiven Guest

    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."
    Unforgiven, Nov 26, 2003
    #3
  4. Pushkar Pradhan

    Rolf Magnus Guest

    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.
    Rolf Magnus, Nov 26, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    466
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    455
    emerth
    Jul 10, 2003
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    390
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. fool
    Replies:
    14
    Views:
    495
    Barry Schwarz
    Jul 3, 2006
  5. jawdoc
    Replies:
    9
    Views:
    738
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page