Having problems with array of pointers.
I have a custom class called C. C has a variable double c1. I have to sort an array of C-s by c1 using a custom written sorting algorithm. I am guessing that since I have to move objects in array, it would be much more eficient to just move pointers to objects, therefore I have to use not an array of objects, but an array of pointers to objects.
I initialized the array like that:
C** someC;
someC = new C*[size];
for(int i = 0; i < size; i++) {
// a and b are of type CPoint
someC[i] = new C(a,b);
}
Am I doing this part correctly? It is the calling of C objects that then causes problems:
someC[i]->a.x
gives me an error: left of ‘->a’ must point to class/struct/union/generic type
I am new to C++ so I may be missing something obvious, but I did some research and did not find anything. Maybe I am not understanding well how pointers work…
UPDATE
The header file of C class:
#pragma once
class C
{
public:
CPoint a;
CPoint b;
double c1;
C(void);
C(CPoint,CPoint);
~C(void);
};
the implementation:
#include "StdAfx.h"
#include "C.h"
#include <math.h>
C::C(void)
{
}
C::C(CPoint a, CPoint b)
{
this->a=a;
this->b=b;
double c1_x = a.x - b.x;
double c1_y = a.y - b.y;
c1= sqrt( (c1_x * c1_x) + (c1_y * c1_y));
}
C::~C(void)
{
}
UPDATE
The problem was in the code I provided in the comments, I did not notice I was calling the array in a wrong way like this:
pDC->MoveTo(someC[i]->a.x, someC->a.y)
So the second call was incorrect. Thank you all for your help
Philosophy aside, this is pretty telling from your comment (emphasis added):
“I am actually calling someC in OnDraw method like that:
pDC->MoveTo(someC[i]->a.x, someC->a.y);someC is defined as public in the header file”Specifically, this in your parameter list :
This tells me one of these is wrong. Judging by your error, I’m going to go with the first one. It would solidify that if we could see the definition of your object that is implementing
OnDraw()and where exactly it is gettingsomeCfrom.someCis aC*in your containing object, the second parameter is correct, the first is wrong.someCis a C** in your contained object then the first parameter is correct and the second is wrong.