?
Solved

Element's of CTypedPtrArray seemed altered

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

649 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