I have a problem with classes, still kind of new to C++ syntax, just moved from 5 years of Delphi (pascal)
I 'think' the class is fine actually, it's a problem with the array, or I have created an instance of the class object wrong ??
ok so heres the class for the above example CrimsonSun suggested.
struct hexCoords{ float x, y, z; float r, g, b, a; float u, v;};class Chexagon{public: // Constructor Chexagon(); // Destructor ~Chexagon(); void draw();protected: // This means that all the variables under this, until a new type of // restriction is placed, will only be accessible to other functions in the // class. hexCoords center, point1, point2, point3, point4, point5, point6;};Chexagon::Chexagon(){ //Constructors can accept arguments, but this one does not}Chexagon::~Chexagon(){ //Destructors do not accept arguments}void Chexagon::draw(){ glBegin(GL_TRIANGLE_FAN); glVertex3f(center.x, center.y, center.z); glVertex3f(point1.x, point1.y, point1.z); glVertex3f(point2.x, point2.y, point2.z); glVertex3f(point3.x, point3.y, point3.z); glVertex3f(point4.x, point4.y, point4.z); glVertex3f(point5.x, point5.y, point5.z); glVertex3f(point6.x, point6.y, point6.z); glVertex3f(point1.x, point1.y, point1.z); glEnd();}
And here is me trying to create an instance and populate the array,
with the hexagonal grid data
void genertaeHexArray(float tileSize){Chexagon hexagon[tilesPerCol][tilesPerRow]; if (tileSize <= 0.0f) // Just an error check { tileSize = 1.0f; } // tile placement & alignment for odd/even rows bool rowOdd = TRUE; // Set TRUE, & will toggle FALSE/TRUE for each Row Loop (Y) // Calculating tile offsets // The magic number //---------------------------------------------------------------- //The amount to budge EVERY tile in a col (except tile 0) float colOffset = 0.866f * tileSize; // The magic number //The amount to budge every row (except tile 0) float rowOffset = 0.75f * tileSize; // The magic number // FILL ARRAY DATA HERE //---------------------------------------------------------------- for (float row = 0; row < tilesPerRow; row++) // ROWS ( Y ) { rowOdd = !rowOdd; // toggle for each row, hence in first loop for (float col = 0; col < tilesPerCol; col++) // COLS ( X ) { if (rowOdd) hexagon[col][row].point1.x = (col * colOffset) + (colOffset/2); hexagon[col][row].point1.y = 0.0f; // Y always 0, flat grid hexagon[col][row].point1.z = (row * rowOffset); else hexagon[col][row].point1.x = (col * colOffset); hexagon[col][row].point1.y = 0.0f; // Y always 0, flat grid hexagon[col][row].point1.z = (row * rowOffset); } }
OK, so now the problem (This seems to be 2-fold ...)
example 1 .....
this line ..
hexagon[col][row].point1.x = (col * colOffset) + (colOffset/2);
causes compile error : subscript is not of integral type
Note I origanlly declared array as
Chexagon hexagon[tilesPerCol * tilesPerRow];
and changed it too
Chexagon hexagon[tilesPerCol][tilesPerRow];
Which is the better way firstly ??
example 2 .....
this line .. (HARDCODING cols and rows to 1 & 2)
hexagon[1][2].point1.x = (col * colOffset) + (colOffset/2);
causes compile error : 'Chexagon::point1' : cannot access protected member declared in class 'Chexagon'
I knew there was a problem with the first one straight away, as it would not show available members after I typed the '.' (period)
But hardcoding array values, it worked.
What have I done wrong ??