Help! Strange problem with pointer to vector

A

Al Newton

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
 
J

John Harrison

Al Newton said:
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
 
A

Al Newton

John Harrison said:
Al Newton said:
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
 
J

John Harrison

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top