Why can't I change objects in a vector?


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:


#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_; };


#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() { }


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


#include "tile.h" Tile::Tile() : isActive_(false) { } Tile::~Tile() { }


#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?


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.


  • jqgrid double headers
  • Getting total View entries including categories - performance issue
  • org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“xp
  • Assert array includes array with Chai
  • priority-web-sdk: fieldUpdate for choose field failed
  • Sort Columns For Dynamic Pivot
  • Testing a python script in a specific version
  • How to send basicAck to inbound adapter after publisher confirm from outbound adapter
  • Liferay: After changing Public Virtual Host settings, can't log in
  • C++ design pattern: multiple ways to load file
  • How to count spring coil turns?
  • Why String.ToString()? [duplicate]
  • Complex Object won't return when using 'include' syntax in WCF with Entity Version 6
  • Required Attribute does not work on foreign key dropdown list
  • Entity framework - Foreign key - data annotation
  • friend declaration in protected section
  • Referring to individual variables in … with dplyr quos
  • Java ClassLoader Confusion
  • SQL Server Integrated Security from an Azure Web Site
  • Is it one-to-one or Component ? Nhibernate Mapping
  • Installing PAR::Packer on Windows, dmake error 255
  • Refresh other frame, from another frame. Jquery
  • Specifying virtual keyboard type for EditText in XML
  • C: Incompatible pointer type initializing
  • javaw.exe and eclipse startup problems
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • MySQL WHERE-condition in procedure ignored
  • vba code to select only visible cells in specific column except heading
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Transpose CSV data with awk (pivot transformation)
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?