?
Solved

Element's of CTypedPtrArray seemed altered

Posted on 2006-06-12
2
Medium Priority
?
471 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 45

Accepted Solution

by:
AndyAinscow earned 600 total points
ID: 16892046
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
ID: 16899144
Great!!
Thanks a lot
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

862 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