list Iterator Dereferencing Problem

M

Mike Copeland

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
 
F

Francesco S. Carta

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.
 
M

Mike Copeland

I know I'm doing something wrong here, but I'm unable to see what it
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.
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top