为什么不能更改向量中的对象? [英] Why can't I change objects in a vector?
问题描述
我有一个 TileGrid
,它包含 std :: vector< std :: vector< Tile> >
。访问向量中的 Tile
对象可以工作,但是我不能更改它们的属性?为了完成,以下是所有相关的类:
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
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
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!
所以是的,我的问题是 setActive
函数。当我创建一个 Tile
并调用它的 setActive
函数,一切正常,但当我通过< c $ c> TileGrid 对象,它不会。
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?
推荐答案
在您的方法中:
Tile atIndex(unsigned int row, unsigned int col) const
您应该返回对Tile的引用:
you should return a reference to Tile:
Tile& atIndex(unsigned int row, unsigned int col)
现在你正在返回副本,这就是为什么修改不工作。也不应该是 const
,否则会得到编译错误。
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.
这篇关于为什么不能更改向量中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!