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.

Polyline.h

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

class CPolyline :
      public CObject
{
public:
      CPolyline(void);
      ~CPolyline(void);

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

Polyline.cpp
#include "StdAfx.h"
#include "polyline.h"

CPolyline::CPolyline(void)
:m_pointArrayIndex(1),
      pSegmentStart(NULL),
      pSegmentEnd(NULL)
{
}

CPolyline::~CPolyline(void)
{
}

void CPolyline::AddPoint(CPoint* pPt)
{
      m_points.Add(pPt);
}

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

      if(m_points.GetCount()>1)
      {      
            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

PhillOAsked:
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.

AlexFMCommented:
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
AlexFMCommented:
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:
vector<CPoint>.
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;
m_points.RemoveAt(0);
PhillOAuthor Commented:
Thanks!

Phill
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.