Question

I have a class TileGrid that holds an std::vector< std::vector<Tile> >. Accessing the Tile objects in the vector works, but I can't change their properties? For the sake of completion, here are all the relevant classes:

tilegrid.h

#include <vector>
#include "tile.h"

class TileGrid {

public:
  TileGrid();
  TileGrid(unsigned int rows, unsigned int cols);
  virtual ~TileGrid();
  unsigned int getRows() const { return rows_; };
  unsigned int getCols() const { return cols_; };
  Tile atIndex(unsigned int row, unsigned int col) const { return tiles_[row].at(col); };

private:
  std::vector< std::vector<Tile> > tiles_;
  unsigned int rows_;
  unsigned int cols_;

};

tilegrid.cpp

#include "tilegrid.h"

TileGrid::TileGrid() : rows_(0), cols_(0) {
}

TileGrid::TileGrid(unsigned int rows, unsigned int cols) : rows_(rows), cols_(cols) {
  tiles_.clear();
  for (unsigned int y = 0; y < rows_; y++) {
    std::vector<Tile> horizontalTiles;
    for (unsigned int x = 0; x < cols_; x++) {
      horizontalTiles.push_back(Tile());
    }
    tiles_.push_back(horizontalTiles);
  }
}

TileGrid::~TileGrid() {
}

tile.h

class Tile {

public:
  Tile();
  virtual ~Tile();
  bool isActive() const { return isActive_; };
  void setActive(bool status) { isActive_ = status; };

private:
  bool isActive_;

};

tile.cpp

#include "tile.h"

Tile::Tile() : isActive_(false) {
}

Tile::~Tile() {
}

main.cpp

#include "tilegrid.h"
#include <iostream>

int main() {

  TileGrid tg(20, 20);

  for (unsigned int i = 0; i < tg.getRows(); i++) {
    for (unsigned int j = 0; j < tg.getCols(); j++) {
      if (tg.atIndex(i, j).isActive()) {
        std::cout << i << "," << j << " is active" << std::endl;
      } else {
        std::cout << i << "," << j << " is NOT active" << std::endl;
      }
    }
  }

  // This is all working. But when I for example use the setActive function, nothing changes:

  tg.atIndex(1, 0).setActive(true);

  // When I print it again, the values are still the ones that were set in the constructor

  for (unsigned int i = 0; i < tg.getRows(); i++) {
    for (unsigned int j = 0; j < tg.getCols(); j++) {
      if (tg.atIndex(i, j).isActive()) {
        std::cout << i << "," << j << " is active" << std::endl;
      } else {
        std::cout << i << "," << j << " is NOT active" << std::endl;
      }
    }
  }

  return 0;

}

I'm really sorry for all this code... I tried to keep it as short as possible, but I thought it'd be better to post it all!

So yeah, my problem is the setActive function. When I just create a Tile and call its setActive function, everything works, but when I call it through the TileGrid object, it won't.

I have tried to solve this on my own for hours and I can't think straight anymore. I'm really desperate here, could you please have a look and maybe help me?

Was it helpful?

Solution

In your method:

Tile atIndex(unsigned int row, unsigned int col) const

you should return a reference to Tile:

Tile& atIndex(unsigned int row, unsigned int col)

now you are returning copy, and that is why modifications does not work. Also it should not be const, otherwise you will get compiler error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top