Solved

Element's of CTypedPtrArray seemed altered

Posted on 2006-06-12
2
419 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 163
not able to insert into temp table 68 169
post4 challenge 28 105
x-bar in Google Sheets 2 72
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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