Why can't I change objects in a vector?

A. D.

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?

marcinj

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't i change a objects attributes from a method

From Dev

Why can't I pass methods as objects?

From Dev

Why can't I dereference pointer to objects?

From Dev

Why can't I pass methods as objects?

From Dev

Why can't I wrap a T* in an std::vector<T>?

From Dev

Why can't I change this BufferedImage?

From Dev

Why can't I change pointers for methods?

From Dev

Why can't I change the value in GridView?

From Dev

Why can't I change toolbar textsize?

From Dev

Why can't I change the value of a Json

From Dev

Why can't I change this BufferedImage?

From Dev

Why I can´t change the wifi card

From Dev

Why can't I change the value in GridView?

From Dev

Why can't I add items into my vector?

From Dev

Why can't I create a vector of threads on the fly like this

From Dev

Why can't I convert a vector into a matrix with 'as.matrix' function?

From Dev

Why can't I access directly in vector iterator?

From Dev

Why can't I push_back to a vector of const elements?

From Dev

Why can't i construct a vector by passing temporary input iterator?

From Dev

Why I can't see any ouput iterating this vector?

From Dev

Why can't I set an iterator on a list in a vector?

From Dev

Why can't I change a set I'm iterating over?

From Dev

Why can't I access my objects member variable?

From Dev

why can't I compare Exception objects for equality?

From Dev

Why can't I make a priority queue of type struct objects?

From Dev

Why can't I access my objects member variable?

From Dev

Why can't I unwrap the root node and deserialize an array of objects?

From Dev

Why I can't get the related objects of my pulgin?

From Dev

Why can't I make a priority queue of type struct objects?

Related Related

HotTag

Archive