?
Solved

Element's of CTypedPtrArray seemed altered

Posted on 2006-06-12
2
Medium Priority
?
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

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. …
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 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