
July 19th, 2005, 04:49 PM
| | | Help! Strange problem with pointer to vector
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 | 
July 19th, 2005, 04:49 PM
| | | Re: Help! Strange problem with pointer to vector
"Al Newton" <alnewton1@hotmail.com> wrote in message
news:4e1067aa.0307281034.22a4b907@posting.google.c om...[color=blue]
> 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.[/color]
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.
[color=blue]
>
> 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[/color]
john | 
July 19th, 2005, 04:49 PM
| | | Re: Help! Strange problem with pointer to vector
"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<bg3qs4$k6nac$1@ID-196037.news.uni-berlin.de>...[color=blue]
> "Al Newton" <alnewton1@hotmail.com> wrote in message
> news:4e1067aa.0307281034.22a4b907@posting.google.c om...[color=green]
> > 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.[/color]
>
> 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.
>[color=green]
> >
> > 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[/color]
>
> john[/color]
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 | 
July 19th, 2005, 04:50 PM
| | | Re: Help! Strange problem with pointer to vector
>[color=blue]
> 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[/color]
Sometimes a necessary evil, but only sometimes.
john |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|