list Iterator Dereferencing Problem

Discussion in 'C++' started by Mike Copeland, Sep 10, 2010.

  1. I have the following structure defined and declared, and I have
    populated the list with 11 records (I watched them go in during debug.)
    After building the list, I'm trying to write it out to a file...but I
    only write 11 copies if the _first_object_instance_ - not the 11
    different objects.
    I know I'm doing something wrong here, but I'm unable to see what it
    is. Please advise. TIA

    struct Tandems
    {
    long tandemLink;
    string teamMate;

    } tandemWork;
    typedef list<Tandems> TandemList;
    TandemList tandemData;
    list<TandemList>::iterator tandemIter;
    ....
    Tandems tanWork; // local work variable
    list<Tandems>::iterator xIter; // local iterator
    for(xIter = tandemData.begin(); xIter != tandemData.end(); xIter++)
    {
    tanWork = (*xIter); // move list object to local variable
    printf("%12d|%s", tanWork.tandemLink,
    tanWork.teamMate.c_str());
    // *** Always seeing the 1st stored object here! 8<{{
    } // for
     
    Mike Copeland, Sep 10, 2010
    #1
    1. Advertising

  2. Mike Copeland <>, on 10/09/2010 15:18:34, wrote:

    > I have the following structure defined and declared, and I have
    > populated the list with 11 records (I watched them go in during debug.)
    > After building the list, I'm trying to write it out to a file...but I
    > only write 11 copies if the _first_object_instance_ - not the 11
    > different objects.
    > I know I'm doing something wrong here, but I'm unable to see what it
    > is. Please advise. TIA
    >
    > struct Tandems
    > {
    > long tandemLink;
    > string teamMate;
    >
    > } tandemWork;
    > typedef list<Tandems> TandemList;
    > TandemList tandemData;
    > list<TandemList>::iterator tandemIter;
    > ....
    > Tandems tanWork; // local work variable
    > list<Tandems>::iterator xIter; // local iterator
    > for(xIter = tandemData.begin(); xIter != tandemData.end(); xIter++)
    > {
    > tanWork = (*xIter); // move list object to local variable
    > printf("%12d|%s", tanWork.tandemLink,
    > tanWork.teamMate.c_str());
    > // *** Always seeing the 1st stored object here! 8<{{
    > } // for


    The piece of code you posted doesn't show the problem you're lamenting.
    Please post a complete, misbehaving program that shows it.

    http://www.parashift.com/c -faq/
    see FAQ 5.8 in particular.

    Chances are that you're inserting always the same object into the list -
    this modification of your program behaves just fine:

    #include <iostream>
    #include <string>
    #include <list>

    using namespace std;

    struct Tandems {
    long tandemLink;
    string teamMate;
    };

    typedef list<Tandems> TandemList;

    int main() {
    TandemList tandemData;

    Tandems t = { 1, "one" };
    Tandems t2 = { 2, "two" };

    tandemData.push_back(t);
    tandemData.push_back(t2);

    Tandems tanWork;
    list<Tandems>::iterator xIter;
    for (xIter = tandemData.begin();
    xIter != tandemData.end();
    xIter++) {
    tanWork = (*xIter);
    cout << tanWork.tandemLink << endl;
    cout << tanWork.teamMate << endl;
    }
    }

    The above prints:

    1
    one
    2
    two

    as expected.

    Besides, try to get rid of those C-isms in your code.

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Sep 11, 2010
    #2
    1. Advertising

  3. > > I know I'm doing something wrong here, but I'm unable to see what it
    > > is. Please advise. TIA
    > >
    > > struct Tandems
    > > {
    > > long tandemLink;
    > > string teamMate;
    > >
    > > } tandemWork;
    > > typedef list<Tandems> TandemList;
    > > TandemList tandemData;
    > > list<TandemList>::iterator tandemIter;
    > > ....
    > > Tandems tanWork; // local work variable
    > > list<Tandems>::iterator xIter; // local iterator
    > > for(xIter = tandemData.begin(); xIter != tandemData.end(); xIter++)
    > > {
    > > tanWork = (*xIter); // move list object to local variable
    > > printf("%12d|%s", tanWork.tandemLink,
    > > tanWork.teamMate.c_str());
    > > // *** Always seeing the 1st stored object here! 8<{{
    > > } // for

    >
    > The piece of code you posted doesn't show the problem you're lamenting.
    > Please post a complete, misbehaving program that shows it.


    Well, the code I posted was cobbled from a rather large program in
    which I was adding this functionality - offering a simple demo was very
    difficult. I tried to show my actual code, but I had to remove some of
    my code (I/o and special subprograms) that wouldn't compile on anyone
    else's system. I apologize...

    > http://www.parashift.com/c -faq/
    > see FAQ 5.8 in particular.
    >
    > Chances are that you're inserting always the same object into the list -
    > this modification of your program behaves just fine:


    Yes, that was it. I believed I saw the code working, but I didn't
    look deep enough into the list objects to see that it wasn't. <sigh...>
    Thanks for the help.
     
    Mike Copeland, Sep 12, 2010
    #3
    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. Replies:
    6
    Views:
    652
    Jim Langston
    Oct 30, 2005
  2. Replies:
    12
    Views:
    5,957
    James Kanze
    Jun 24, 2007
  3. David Bilsby
    Replies:
    5
    Views:
    2,048
    David Bilsby
    Oct 9, 2007
  4. Mike Copeland

    list Iterator Dereferencing Problem

    Mike Copeland, Sep 10, 2010, in forum: C++
    Replies:
    4
    Views:
    761
    Joshua Maurice
    Sep 12, 2010
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    131
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page