Solved

Element's of CTypedPtrArray seemed altered

Posted on 2006-06-12
2
388 Views
Last Modified: 2013-11-20
Hi,
I am having trouble to build an CTypedPtrArray with Add function/

I am using a CArray having redundant values and copied to CTypedPtrArray to make Probability Distribution Table.
aryTD is a original CArray of int values are stored.
aryProDist is a CTypedPtrArray of a Class called CProDist.
The Class CProDist has three member variables,  constructor with three argument to assign to it's member variables, and copy constructor.
The Following piece of code shows Different values of the same CTypedPtrArray aryProDist.
What am I doing wrong?
Why all elements of aryProDist filled with last one?

     CTypedPtrArray<CObArray, CProDist<int>*> aryProDist;
      CArray<int, int> aryTD;

                .............

      TRACE("After QSort aryTD size : %d\n", aryTD.GetSize());
      for(i = 0; i<aryTD.GetSize(); i++)
      {
            TRACE("%d\n", aryTD[i]);
      }

      iCount = 0;
      for(i = 0; i<aryTD.GetSize(); i++)
      {
            
            if(i == 0 || (i !=0 && aryTD[i] != aryTD[i-1]))
            {
                  iValue = aryTD[i];
                  float fPD = (float)((float)iCount/(float)aryTD.GetSize());
                  CProDist<int>      *pd = &CProDist<int>(iValue, ++iCount, fPD);
                  aryProDist.Add(pd);
                  TRACE("Adding ...%d, %d, %d, %f\n", aryProDist.GetSize()-1, aryProDist[aryProDist.GetSize()-1]->m_tValue,
                                               aryProDist[aryProDist.GetSize()-1]->m_iNumValue, aryProDist[aryProDist.GetSize()-1]->m_fProDist);
            }
     }

    TRACE("After Add\n");
      for(int k=0; k<aryProDist.GetSize(); k++)
      {
            CProDist<int> *pp = aryProDist.GetAt(k);
            TRACE("%d, %d, %d, %f\n", k, pp->m_tValue, pp->m_iNumValue, pp->m_fProDist);
      }


Results Window Shows like this.

After QSort aryTD size : 7
11
12
12
15
17
17
20
Adding ...0, 11, 1, 0.000000
Adding ...1, 12, 2, 0.142857
Adding ...2, 15, 3, 0.285714
Adding ...3, 17, 4, 0.428571
Adding ...4, 20, 5, 0.571429
After Add
0, 20, 5, 0.571429
1, 20, 5, 0.571429
2, 20, 5, 0.571429
3, 20, 5, 0.571429
4, 20, 5, 0.571429
0
Comment
Question by:qkrwngml
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 200 total points
Comment Utility
Create a new instance of CProDist (*pd = new....) and add that to the typed pointer array.
What you have done is add the SAME item five times, not five different items.
0
 

Author Comment

by:qkrwngml
Comment Utility
Great!!
Thanks a lot
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now