Help! Strange problem with pointer to vector

Discussion in 'C++' started by Al Newton, Jul 28, 2003.

  1. Al Newton

    Al Newton Guest

    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
     
    Al Newton, Jul 28, 2003
    #1
    1. Advertising

  2. "Al Newton" <> wrote in message
    news:...
    > 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
     
    John Harrison, Jul 28, 2003
    #2
    1. Advertising

  3. Al Newton

    Al Newton Guest

    "John Harrison" <> wrote in message news:<bg3qs4$k6nac$-berlin.de>...
    > "Al Newton" <> wrote in message
    > news:...
    > > 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
     
    Al Newton, Jul 28, 2003
    #3
  4. >
    > 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
     
    John Harrison, Jul 29, 2003
    #4
    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. Sean Dettrick
    Replies:
    2
    Views:
    413
    Sean Dettrick
    Jul 31, 2003
  2. pmatos
    Replies:
    6
    Views:
    24,122
  3. Replies:
    8
    Views:
    1,998
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    621
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    395
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page