为什么我不能更改矢量中的对象?
Why can't I change objects in a vector?
我有一个类TileGrid
,它有一个std::vector< std::vector<Tile> >
。访问矢量中的Tile
对象有效,但我无法更改它们的属性?为了完成,以下是所有相关类:
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_;
};
瓷砖网格.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() {
}
磁贴.h
class Tile {
public:
Tile();
virtual ~Tile();
bool isActive() const { return isActive_; };
void setActive(bool status) { isActive_ = status; };
private:
bool isActive_;
};
磁贴.cpp
#include "tile.h"
Tile::Tile() : isActive_(false) {
}
Tile::~Tile() {
}
主.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;
}
我真的很抱歉所有这些代码...我试图让它尽可能简短,但我认为最好全部发布!
所以是的,我的问题是setActive
函数。当我只是创建一个Tile
并调用其setActive
函数时,一切正常,但是当我通过TileGrid
对象调用它时,它不会。
我已经尝试自己解决这个问题几个小时了,我不能再直截了当地思考了。我在这里真的很绝望,你能不能看看,也许可以帮助我?
在你的方法中:
Tile atIndex(unsigned int row, unsigned int col) const
应返回对磁贴的引用:
Tile& atIndex(unsigned int row, unsigned int col)
现在您正在返回副本,这就是修改不起作用的原因。也不应该const
,否则你会得到编译器错误。
相关文章:
- 为什么模板类中的对象不能返回值
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 如果'C'公开继承'B',B 私下继承'A',为什么我不能在"C"中创建"A"的对象?
- 为什么我不能在主函数之外定义一个类的对象(它继承了另一个类)?
- 为什么我不能引用指向实例化对象的函数的指针?
- Visual Studio 2017 C++,不能使用 typeid() 获取信息对象,缺少指针?;
- 为什么我在声明对象数组时不能使用 -> 运算符?
- 可变大小的对象不能被初始化混淆
- 静态初始化的对象不能放在块中尝试
- Ofstream 对象不能使用运算符<<C++
- 为什么C++左值对象不能绑定到右值引用 (&&)?
- 创建一个类时出错,该类的对象不能在堆栈上创建,只能在堆上创建
- 为什么易失性对象不能调用非易失性成员函数
- 编译错误c++:没有对象不能调用成员函数
- 对象不能在SDL (c++)中工作
- 私有继承的派生类的对象不能访问基类成员函数
- Std::for_each, operator()重载的对象不能维持状态
- c++函数对象不能使用std::for_each (VS2012)创建sum
- 为什么用A{8}初始化的对象不能作为参数传递