473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Jul 19 '05 #1
3 6123

"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
"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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Helmut Jarausch | last post by:
Hi, entering help('rstrip') or help('ljust') into IDLE's shell window I only get no Python documentation found ...
2
by: clintonG | last post by:
Is there a document that explains the difference between the Visual Studio .NET 2003 Combined Help Collection and the MSDN Library for Visual Studio .NET 2003 or does someone have comments...
2
by: John Baker | last post by:
I find it highly annoying that MS Access tries to go online when I want to look at the help files. Is there a way to configure it so it just looks at my local helpfiles when I hit F1?
27
by: Bruce Dodds | last post by:
I recently started using Access 2003 for the first time. I wanted to pass on some comments about the Help system to Access MVPs who frequent this board. I'm doing this in the hope that some of...
5
by: Steve Teeples | last post by:
Can someone point me to a document that clearly identifies the steps of creating a good help system for an application? I have a test tool that I'd like to add help to so that others will know how...
3
by: stuart_white_ | last post by:
I've just upgraded from Python 2.3.3 to Python 2.4.2, and, although the new version of Python seems to be running correctly, I can't seem access the help from the interpreter. On Python 2.3.3...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
5
by: PaulS | last post by:
new to Fedora7, typed python in interactive interpreter, then help(). Then modules to get a list of modules. Then module name to get info on a module but no help file. What is the help file...
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.