二维矢量增量错误

2d vector incrementation error

本文关键字:错误 二维      更新时间:2023-10-16

我正在尝试创建一个简单的扫雷游戏,并且在创建棋盘时遇到了一些问题。我正在使用 2d 矢量代替 2d 数组,并且在增加图块值以查看与图块相邻的地雷数量时遇到问题。

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 = 真,如果是,则游戏结束。如果否,则 isReveal 设置为 true,磁贴显示与磁贴相邻的地雷数。但是,我收到错误:

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

是否需要设置单独的函数来递增内容?我假设 board.resize 填充了充满 0 的向量。我很感激你的帮助。

以下是"磁贴"文件的内容:

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++