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

Help! Strange problem with pointer to vector

P: n/a
My class has this member function:

vector<UniqueCustId*>* CMyClass::GetCustList( void )
{
SYSTEMTIME st;
string strServiceDate;
UniqueCustId* uci = new UniqueCustId;
string strCustNumberOld = "0";

for ( int i = 0; i < m_ParsedRecords.size(); ++i )
{
uci->CustNumber = m_ParsedRecords[ i ].strCustNumber;
strServiceDate = m_ParsedRecords[ i ].strDateOfService;
st = ConvertDateToSystemTime( strServiceDate );
uci->ServiceDate = st;
if ( uci->CustNumber != strCustNumberOld )
{
m_UniqueCust->push_back( uci );
strCustNumberOld = uci->CustNumber;
}
else
{
continue;
}
}

string strCustNbr01 = (*m_UniqueCust)[ 0 ]->CustNumber; // Test
string strCustNbr02 = (*m_UniqueCust)[ 1 ]->CustNumber; // Ditto

return m_UniqueCust;
}

The intent is to loop through m_ParsedRecords (a private member
variable declared as: vector<RECORD> m_ParsedRecords) and store only
unique customer numbers in m_UniqueCust (a private member variable
declared as:
vector<UniqueCustId*>* m_UniqueCust). This last variable is declared
in an initialize function as: m_UniqueRx = new vector<UniqueRxId*>;

My problem is that by setting a breakpoint on the push_back line, I
see unique numbers going into the vector. But ... if I breakpoint the
strings before the return I see only the last number! The function
that calls GetCustList() also sees a vector containing that same
number repeated over and over and over.

I've looked at this code until my eyes glaze and I see nothing wrong
with the logic. Could someone *please* tell me what's wrong. Many
thanks ... Al
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Al Newton" <al*******@hotmail.com> wrote in message
news:4e**************************@posting.google.c om...
My class has this member function:

vector<UniqueCustId*>* CMyClass::GetCustList( void )
{
SYSTEMTIME st;
string strServiceDate;
UniqueCustId* uci = new UniqueCustId;
string strCustNumberOld = "0";

for ( int i = 0; i < m_ParsedRecords.size(); ++i )
{
uci->CustNumber = m_ParsedRecords[ i ].strCustNumber;
strServiceDate = m_ParsedRecords[ i ].strDateOfService;
st = ConvertDateToSystemTime( strServiceDate );
uci->ServiceDate = st;
if ( uci->CustNumber != strCustNumberOld )
{
m_UniqueCust->push_back( uci );
strCustNumberOld = uci->CustNumber;
}
else
{
continue;
}
}

string strCustNbr01 = (*m_UniqueCust)[ 0 ]->CustNumber; // Test
string strCustNbr02 = (*m_UniqueCust)[ 1 ]->CustNumber; // Ditto

return m_UniqueCust;
}

The intent is to loop through m_ParsedRecords (a private member
variable declared as: vector<RECORD> m_ParsedRecords) and store only
unique customer numbers in m_UniqueCust (a private member variable
declared as:
vector<UniqueCustId*>* m_UniqueCust). This last variable is declared
in an initialize function as: m_UniqueRx = new vector<UniqueRxId*>;

My problem is that by setting a breakpoint on the push_back line, I
see unique numbers going into the vector. But ... if I breakpoint the
strings before the return I see only the last number! The function
that calls GetCustList() also sees a vector containing that same
number repeated over and over and over.
Well of course, you are repeatedly pushing the _same_ pointer onto your
vector.

Either don't pointers, (usually the sensible solution, but newbies love
pointers even though they often don't understand how to handle them).
Alternatively make sure that you allocate a different UniqueCustId each time
you add an item to your unique vector.

I've looked at this code until my eyes glaze and I see nothing wrong
with the logic. Could someone *please* tell me what's wrong. Many
thanks ... Al


john
Jul 19 '05 #2

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bg************@ID-196037.news.uni-berlin.de>...
"Al Newton" <al*******@hotmail.com> wrote in message
news:4e**************************@posting.google.c om...
My class has this member function:

vector<UniqueCustId*>* CMyClass::GetCustList( void )
{
SYSTEMTIME st;
string strServiceDate;
UniqueCustId* uci = new UniqueCustId;
string strCustNumberOld = "0";

for ( int i = 0; i < m_ParsedRecords.size(); ++i )
{
uci->CustNumber = m_ParsedRecords[ i ].strCustNumber;
strServiceDate = m_ParsedRecords[ i ].strDateOfService;
st = ConvertDateToSystemTime( strServiceDate );
uci->ServiceDate = st;
if ( uci->CustNumber != strCustNumberOld )
{
m_UniqueCust->push_back( uci );
strCustNumberOld = uci->CustNumber;
}
else
{
continue;
}
}

string strCustNbr01 = (*m_UniqueCust)[ 0 ]->CustNumber; // Test
string strCustNbr02 = (*m_UniqueCust)[ 1 ]->CustNumber; // Ditto

return m_UniqueCust;
}

The intent is to loop through m_ParsedRecords (a private member
variable declared as: vector<RECORD> m_ParsedRecords) and store only
unique customer numbers in m_UniqueCust (a private member variable
declared as:
vector<UniqueCustId*>* m_UniqueCust). This last variable is declared
in an initialize function as: m_UniqueRx = new vector<UniqueRxId*>;

My problem is that by setting a breakpoint on the push_back line, I
see unique numbers going into the vector. But ... if I breakpoint the
strings before the return I see only the last number! The function
that calls GetCustList() also sees a vector containing that same
number repeated over and over and over.


Well of course, you are repeatedly pushing the _same_ pointer onto your
vector.

Either don't pointers, (usually the sensible solution, but newbies love
pointers even though they often don't understand how to handle them).
Alternatively make sure that you allocate a different UniqueCustId each time
you add an item to your unique vector.

I've looked at this code until my eyes glaze and I see nothing wrong
with the logic. Could someone *please* tell me what's wrong. Many
thanks ... Al


john


Moving the pointer allocation inside the for loop did the trick.
Thanks for the suggestion. I'm finally starting to believe that
pointers are evil.

.... Al
Jul 19 '05 #3

P: n/a
>
Moving the pointer allocation inside the for loop did the trick.
Thanks for the suggestion. I'm finally starting to believe that
pointers are evil.

... Al


Sometimes a necessary evil, but only sometimes.

john
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.