I am attempting to create a grid of hexagons on a windows form.
To do this i have created a 'hexagon' class with the header file as follows:
ref class Hexagon
{
public:
Hexagon(int X, int Y, int I, int J);
Hexagon();
private:
array<Point>^ vertices;
Point Centre;
int i, j;
public:
int GetX();
int GetY();
void SetCentre(int X, int Y);
void CalculateVertices();
array<Point>^ GetVertices();
void drawHexagon();
};
i then want to have a 2 dimensional array storing these hexagons. my attempt at doing this is as follows:
array<Hexagon^,2>^ Grid
but i get the 'a variable with static storage duration cannot have a handle or tracking reference type'
how do i create a 2d array to add hexagons to?
A ref class declares a class which is managed by the garbage collector. One strong restriction that the C++/CLI compiler applies to such declarations is that such class cannot contain non-managed objects. That very often turns out poorly when the object is moved when the heap is compacted, invalidating unmanaged pointers to such non-managed objects.
The likely trouble-maker is the Point
type, there are no other candidates. A sample declaration for a managed Point type that doesn't have this problem is:
public value struct Point {
int x, y;
};
Or use the baked-in System::Drawing::Point type instead.