-2

I have a list of 'cell' objects in a 2d array of length [sizeX][sizeY]. These Cell objects contain an array of type *Cell, which should point to each of the given cell's adjacent cells in the format North, East, South, West (Never Eat Soggy Waffles).

This array is called compass, and is defined with a length of 4. After the cells have been initialized (at which time all values of compass are set to nullptr), I have a loop which attempts to fill Cell.compass with pointers to the appropriate nearby cells.

However, I have found that despite all this, in each cell I have found that compass still is full of null values.

In this loop, I also run a function Cell::computeTopology() which populates a vector (which is a property of Cell) of the valid non-null indexes of compass. This is similarly empty.

I have made a breakpoint both before and after this function has been called to the exact same effect. Regardless I will include this function as well. I'm utterly perplexed and some help here would be greatly appreciated.

Problem area:

  const int sizeX = SCREEN_WIDTH / SCALE;
    const int sizeY = SCREEN_HEIGHT / SCALE;
    Cell cells[sizeX][sizeY];
    for (int x = 0; x < sizeX; x++){
        for (int y = 0; y < sizeY; y++){
            cells[x][y].setPos(x, y);
            cells[x][y] = Cell();
            //cells[x][y].setColor(rand() % 255, rand() % 255, rand() % 255);
        }
    }
    for (int x = 0; x < sizeX; x++) {
        for (int y = 0; y < sizeY; y++) {
            Cell c = cells[x][y];
            if (x - 1 >= 0) {

                c.compass[3] = &cells[x - 1][y];
            }
            if (x + 1 < (SCREEN_WIDTH / SCALE)) {

                c.compass[1] = &cells[x + 1][y];

            }
            if (y - 1 >= 0) {

                c.compass[0] = &cells[x][y - 1];

            }
            if (y + 1 < (SCREEN_HEIGHT / SCALE)) {

                c.compass[2] = &cells[x][y + 1];
            }
            c.computeTopology();
        }
    }

And the computeTopology() function

void Cell::computeTopology()
{
    int i = 0;
    for (Cell *c : compass) {
        if (c != nullptr) {
            notNull.push_back(i);
            i++;
        }
    }
}
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
dlarkr
  • 3
  • 4

1 Answers1

0

Change

Cell c = cells[x][y]; // make a copy

to

Cell& c = cells[x][y];

you want to modify item of array, not copy.

Another issue

cells[x][y].setPos(x, y);
cells[x][y] = Cell(); // ?

you are setting some members of cells[x][y] by calling setPos and after it you are overwriting cells[x][y] by default constructed Cell object. I think the second line in above code should be removed.


Your words

which populates a vector (which is a property of Cell) of the valid non-null indexes of compass

so i index should be advanced with every iteration of for loop:

  int i = 0;
  for (Cell *c : compass) {
        if (c != nullptr) {
            notNull.push_back(i);
        }
        ++i; // moved
  }
rafix07
  • 20,001
  • 3
  • 20
  • 33
  • Well we also don't know what `cells[x][y] = Cell();` does. After he calls `cells[x][y].setPos(x,y)` he could be wiping it out with that call. – jiveturkey Sep 20 '18 at 18:10
  • Yes I am aware that cells[x][y] = Cell(); is redundant but I was just trying to cover all bases... thanks though. your answer makes a lot of sense.Gonna try it rn – dlarkr Sep 20 '18 at 18:27
  • also setpos doesnt actually do anything anymore. this is a very large project and unfortunately yeah there is quite a bit of fluff that needs to be cleaned. THANKS SO MUCH – dlarkr Sep 20 '18 at 18:30
  • Eh although your correction to the first set of code was correct, by "valid non-null indexes" I meant the indexes of compass that did result in nullpointers. My code works as intended with computeTopology as it was, and I feel like my specifications were worded pretty clearly. – dlarkr Sep 20 '18 at 18:50
  • @DrewCarlisle I suppose compass has 4 elements, then you want to store indices to pointers which are not null, so incrementing `i` only if c is not null, is incorrect. `[][not-null][][not-null]`, so you want to store 1 and 3, not 0 and 1. Or I have misunderstood something. – rafix07 Sep 20 '18 at 18:52
  • I always want to increment i, because I want to store the indexes as they relate to compass. (in laymans terms I want to save the indexes that I can reliably reference in compass - its the only way I can tell if my cell is on the upper left corner or the upper right for example) – dlarkr Sep 20 '18 at 19:20