c++ - 二维 vector 增量误差

标签 c++ vector

我正在尝试创建一个简单的扫雷游戏,但在创建棋盘时遇到了一些问题。我正在使用 2d vector 代替 2d 数组,并且在递增 tiles 值以查看有多少地雷与 tile 相邻时遇到了问题。

int Boardsize::createBoard() const {
//  vector < vector<Tile> > board;
impl->board.resize(getLength(), vector<Tile>(getWidth(), Tile()));
for (int i = 0; i < getMines(); i++) {
    int v1 = rand() % getLength();
    int v2 = rand() % getWidth();
    if (impl->board[v1][v2].getMine() == true) i--;
    else  {impl->board[v1][v2].setMine(true);
          if (v1 - 1 > -1) impl->board[v1-1][v2]++;
          if (v1 + 1 < getLength()) impl->board[v1+1][v2]++;
          if (v2 - 1 > -1) impl->board[v1][v2-1]++;
          if (v2 + 1 < getWidth()) impl->board[v1][v2+1]++;
          if ((v1 - 1 > -1) && (v2 - 1 > -1)) impl->board[v1-1][v2-1]++;
          if ((v1 - 1 > -1) && (v2 + 1 < getWidth())) impl->board[v1-1][v2+1]++;
          if ((v1 + 1 < getLength()) && (v2 - 1 > -1)) impl->board[v1+1][v2-1]++;
          if ((v1 + 1 < getLength()) && (v2 + 1 < getWidth())) impl->board[v1+1][v2+1]++;
        }
    }
}

值长度、宽度和地雷是提前设置的。我打算它的工作方式是“检查 getMine = true,如果是,则游戏结束。如果不是,则 isRevealed 设置为 true,并且图 block 显示有多少地雷与图 block 相邻。但是,我收到错误:

error: no 'operator++(int)' declared for postfix '++' [-fpermissive]|

我是否需要设置一个单独的函数来增加内容?我假设 board.resize 填充了全为 0 的 vector 。 感谢您的帮助。

这是“Tile”文件的内容:

namespace Minesweeper {
using namespace std;

class Tile::Tilement {
    int status;
    bool mine;
    int Adjmines;
    friend class Tile;
public:
    Tilement ()
    : status(0), mine(false), Adjmines(0)
    {
    }
};

Tile::Tile() {
    cout << "Tile is being created" << endl;
}
Tile::~Tile() {
    cout << "Tile is being deleted" << endl;
}

void Tile::setMine(int a) {
    tint->mine = true;
}

void Tile::setStatus(int a) {
    if ((a == 0) || (a == 1) || (a == 2)) {
        tint->status = a;
    }
    else {
        #ifdef DEBUG
        cout << "Tile status invalid" << endl;
        #endif // DEBUG
        throw invalid_argument("Invalid tile status");
    }
}

//void Tile::setContent(char r) {
//    tint->content = r;
//}

int Tile::getStatus() const {
    return tint->status;
}

char Tile::getAdjcount() const {
    return tint->Adjmines;
}

char Tile::getMine() const {
    return tint->mine;
}

int Tile::setAdjmines(int a) {
    a = a++;
}

char Tile::getContent() const {
    if (Tile::getMine() == true) {
        return tint->mine;
    }
    else return tint->Adjmines;
}

编辑:我稍微改变了增量,现在它们是这样的:

if (v1 - 1 > -1) impl->board[v1-1][v2].incAdjmines; (etc).

incAdjmines 函数如下所示:

int Tile::incAdjmines() {
Adjmines = Adjmines + 1;
}

而且...好吧,代码已编译,如果没有别的,但由于另一段代码中的一些错误,我无法判断它是否正常工作。感谢大家迄今为止的帮助。

最佳答案

您正在 Tile 对象上调用 ++,该运算符似乎没有重载。 您可以通过为 Tile 类重载此运算符来解决您的问题。或者直接告诉您要增加哪个变量,例如:

impl->board[v1-1][v2].cout_of_things++

关于c++ - 二维 vector 增量误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34900278/

相关文章:

具有异构构造函数约束的 C++ 工厂模式

c++ - 如何优化 VBO/IBO 以最大化 GPU 缓存使用

c++ - 将 double * 赋值给 V[3]

c++ - 对 vector 使用[] []运算符?

python - 在 Numpy 上优化向量归一化

c++ - 在 C++ 中重新定义头文件之间的虚函数

c++ - 实验性 make_array,我可以使用 brace-init 列表作为参数吗?

C++ 将二进制数组转换为类

c++ - 对新运算符的 undefined reference

c++ - vector 括号语法与迭代器