c++ - 为什么我不能更改 vector 中的对象?

标签 c++ constructor setter getter

我有一个类TileGrid持有 std::vector< std::vector<Tile> > .访问 Tile vector 中的对象有效,但我无法更改它们的属性?为了完成,这里是所有相关的类:

瓷砖网格.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 的引用:

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

现在您要返回拷贝,这就是修改不起作用的原因。而且它不应该是 const,否则你会得到编译错误。

关于c++ - 为什么我不能更改 vector 中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21141168/

相关文章:

c++ - C++中模板转换 `operator const T &`的属性是什么?

objective-c - 如何将类名作为参数传递给 cocoa 中的对象工厂?

c++ - 返回对对象构造函数的引用

ruby - setter方法的执行时机

c++ - 从 Getline 解析空格分隔的数字

c++ - QuantLib 构建错误

c++ - 一个生产者线程,多个消费者

java - 如何通过另一个构造函数添加到数组列表?

c# - 为什么派生类无法从基类访问 protected getter?

java - setter 或 getter 中的逻辑