C++ 0x00D64674处的未处理异常 #包括“Cell.h” #包括 使用名称空间std; 空单元格::CreateArea(整数x[][100],整数y[][100],整数f,整数c,整数tc){ int f1,c1; 对于(int i=-1;i

C++ 0x00D64674处的未处理异常 #包括“Cell.h” #包括 使用名称空间std; 空单元格::CreateArea(整数x[][100],整数y[][100],整数f,整数c,整数tc){ int f1,c1; 对于(int i=-1;i,c++,C++,数组从0到n-1,因此CreateArea循环不正确: #include "Cell.h" #include <iostream> using namespace std; void Cell::CreateArea(int x[][100], int y[][100], int f, int c, int tc){ int f1, c1; for (int i = -1; i <= f; i++){ for (int j = -1; j <= c; j++)

数组从
0
n-1
,因此
CreateArea
循环不正确:

#include "Cell.h"
#include <iostream>

using namespace std;

void Cell::CreateArea(int x[][100], int y[][100], int f, int c, int tc){
int f1, c1;
for (int i = -1; i <= f; i++){
    for (int j = -1; j <= c; j++){
        x[i][j] = 0;
        y[i][j] = 0;
    }
}
//Copy(y,x,f,c); //copy the matrix of zeros in another temporary array
while (tc){
    bool re = true;
    while (re){
        f1 = rand() % f;
        c1 = rand() % f;
        if (x[f1][c1] == 0)
        {
            x[f1][c1] = 1; //enter random cells in random positions that are not repeated
            re = false;
        }
    }
    tc--; //decreases the umber of cells to be entered
  }
}

void Cell::PrintArea(int x[][100], int f, int c){
for (int i = 0; i<f; i++){
    for (int j = 0; j<c; j++){
        if (x[i][j] == 1) cout << '*'; //Prints a "*" if cells are alive 
        else cout << '.'; //Prints a "." if cells are dead 
    }
    cout << endl;
  }
}

int Cell::Population(int x[][100], int f, int c){
int counter = 0;
for (int i = 0; i<f; i++)
for (int j = 0; j<c; j++)
if (x[i][j] == 1)counter++;
return counter;

for(inti=-1;i“你已经做了几个小时了!”-真的-只是刚刚看到了这个问题:-)但是为什么不使用调试器呢?构建一个调试版本,并在调试器中运行。它将告诉您崩溃发生的位置,并让您检查和遍历函数调用堆栈以及检查变量的值。还有,为什么不使用vectorsMy guess呢?您正在写入数组的边界之外。这一次,您正在写入负索引。同样,在so中您使用的me案例,例如
i
和其他
i在C/C++中长度
n
的数组从
0…(n-1)合法索引
.Period。利用这一点信息来修正你的代码,确保规则永远不会被违反。他到底为什么要在里面放一个
-1
?非常感谢你们两位!谢谢你们让我过上了这一年。第一篇确实帮助了meGood的帖子。然后,请投票并标记为正确答案;-)
for (int i = -1; i <= f; i++){
    for (int j = -1; j <= c; j++){
for (int i = 0; i < f; i++){
    for (int j = 0; j < c; j++){