二维矢量增量错误
2d vector incrementation error
我正在尝试创建一个简单的扫雷游戏,并且在创建棋盘时遇到了一些问题。我正在使用 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++
相关文章:
- 迭代二维矢量时发生运行时错误
- 尝试从动态分配的二维数组 C++ 中读取值时出现分段错误,并在尝试删除它时给出 munmap_chunk():
- 二维数组错误
- 创建二维向量的错误
- 错误:解除分配二维阵列
- 将二维数组传递给函数会产生奇怪的错误
- 二维数组分割错误
- 二维矢量增量错误
- 值错误的二维矢量
- 二维数组索引中的c++错误
- 二维矢量的运行时错误
- 访问传递给函数的二维数组时出现分段错误
- 为什么在尝试用c++创建二维数组时会出现此错误
- c++二维向量语法错误
- 当我尝试输出一个二维数组时,它输出了错误的数字
- 正在将内存分配给二维字符数组,错误:从“void*”到“char**”的转换无效
- 将二维数组推入矢量并删除数组,但会导致分割错误
- 在建立二维矩阵时避免分割错误
- 二维数组分配中的c++分割错误
- 指向二维数组的指针,程序编译时抛出分段错误