Hello All,
I am having trouble looping through a 2D array in C++ (should be very easy, but I am just starting C++). At the current moment I have this class file
#include "Cube.h"
#include <freeglut.h>
#include <stdio.h>
Cube::Cube(int cx, int cy, int cz , int cubeWidth)
{
int p[][3] = { { cx, cy, cz }, { cx, cy-cubeWidth, cz }, { cx + cubeWidth, cy - cubeWidth, cz },{cx + cubeWidth, cy, cz},
{ cx, cy, cz - cubeWidth }, { cx, cy-cubeWidth, cz - cubeWidth }, { cx+cubeWidth, cy-cubeWidth, cz - cubeWidth }, { cx+cubeWidth, cy, cz - cubeWidth } };
points = *p;
int faces[][4] = { { 0, 1, 2, 3 }, { 3, 2, 6, 7 }, { 7, 6, 5, 4 }, {4, 5, 1, 0 }, {0, 3, 7, 4}, { 1, 5, 6, 2} };
}
bool printed = false;
void Cube::drawCube(){
if (!printed){
glBegin(GL_POLYGON);
for (int i = 0 ; i != 24; i++){
printf("%d: %d, ", i, *(points + i));
if (i % 3 == 0)
printf("\n");
}
printed = true;
glEnd();
}
}
Cube::~Cube()
{
}
amd this header file:
#pragma once
class Cube
{
private:
int *points;//cd array containing all the points of the cube
int points2[8][3];
int *faces;
public:
Cube(int, int, int, int);
~Cube();
void drawCube();
};
As you can see, I tried looping through that way, but what the program prints out doesn't make any sense. I am wondering how I can loop through this array, or if there is any way to store the 2D array as an array then that would work too. When I tried that it gave me a "expression must be a modifiable lvalue" error. I can assign them 1 by one, but I am wondering if there is simpler way of defining them.
Thank you for your help!