Custom Class

I am trying to create a custom class for a Uni assignment.

The class is inteded to hold a collection of points so it will draw a polyline ie a number of connected lines.


#pragma once
#include "afx.h"
#include "atltypes.h"

class CPolyline :
      public CObject

      void AddPoint(CPoint* pPt);
      CPtrArray m_points;//Collection of Point Objects
      int m_pointArrayIndex;
      CPoint* pSegmentStart;
      CPoint* pSegmentEnd;
      // Draws a complete polyline
      void DrawPolyLine(CDC* pDC);

#include "StdAfx.h"
#include "polyline.h"



void CPolyline::AddPoint(CPoint* pPt)

void CPolyline::DrawPolyLine(CDC* pDC)//redraws a polyline

            for (int i=0; i < m_points.GetCount(); i++)
*******            pSegmentStart = m_points.GetAt(i-1);        *******
*******        pSegmentEnd = m_points.GetAt(i);            *******

                  pDC->MoveTo(pSegmentStart->x, pSegmentStart->y);
                  pDC->LineTo(pSegmentEnd->x, pSegmentEnd->y);

The Lines above denoted by ***etc are giving me compile errors

error C2440: '=' : cannot convert from 'void *' to 'CPoint *'

I have used CPtrArray because it works but why can't  i use CObArray??

I am not very good at c++ programming and would appreciate some quick (and patient) responses

Regards Phill

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPtrArray works with void* members. To read member of m_points, you need to case them to CPoint*.

pSegmentStart = (CPoint*)m_points.GetAt(i-1);        
pSegmentEnd = (CPoint*)m_points.GetAt(i);            

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Instead of using MFC containers, it is better to work with type-safe STL containers. For example, STL m_points can have type vector<CPoint*>.
CPoint class has only two members, and is easy to create on stack and copy. For such classes, like for simple types (int, char ...) it is better to keep objects and not pointers:
This prevents memory leaks.
In your current code, remember that if you keep pointers in container, you must release it when element is removed from container:

m_points.Add(new CPoint(0,0));
CPoint* pt = (CPoint*)m_points.GetAt(0);        
delete pt;
PhillOAuthor Commented:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.