我试图将2维元胞自动机从处理转换为openFrameworks(C++)。我已经为单元格和生命游戏功能编写了类。应用程序生成成功,但立即崩溃并出现以下错误:线程1:编程接收到的信号:“EXC_BAD_ACCESS”。C++新手,“EXC_BAD_ACCESS”错误我不明白
这里是我的我的生活类
#include "Cell.h"
class GoL {
public:
GoL();
void init();
void generate();
void display();
void run();
int w = 20;
int cols;
int rows;
std::vector<vector<cell> > board;
};
这里的游戏标题内实现:
#include "GoL.h"
GoL::GoL() {
cols = ofGetWidth()/w;
rows = ofGetHeight()/w;
board[rows][cols];
init();
}
void GoL::run() {
generate();
display();
}
void GoL::init() {
for (int i = 0; i < cols; i ++) {
for (int j = 0; j < rows; j ++) {
board[i][j] = *new cell(i * w, j * w, w);
}
}
}
void GoL::generate() {
for (int i = 0; i < cols; i ++) {
for (int j = 0; j < rows; j ++) {
board[i][j].savePrevious();
}
}
for (int x = 0; x < cols; x ++) {
for (int y = 0; y < cols; y ++) {
int neighbours = 0;
for (int i = -1; i <= 1; i ++) {
for (int j = -1; j <= 1; j ++) {
neighbours += board[(x + i + cols) % cols][(y + j + rows) % rows].previous;
}
}
neighbours -= board[x][y].previous;
// Rules of Life
if ((board[x][y].state == 1) && (neighbours < 2)) board[x][y].newState(0);
else if ((board[x][y].state == 1) && (neighbours > 3)) board[x][y].newState(0);
else if ((board[x][y].state == 0) && (neighbours == 3)) board[x][y].newState(1);
}
}
}
void GoL::display() {
for (int i = 0; i < cols; i ++) {
for (int j = 0; j < rows; j ++) {
board[i][j].display();
}
}
}
错误在vector.h文件显示出来,在GoL的头文件以及我在GoL实现中调用init()方法的地方。任何帮助深表感谢。
你没有在你的构造函数中正确初始化你的电路板矢量。你不需要用'new'来初始化每个单元格。改为使用临时和复制结构。 – jxh 2013-02-25 19:05:45
'board [rows] [cols];'你在那里意味着什么? – 2013-02-25 19:06:35
这种类型的问题只有一个很好的答案 - 使用调试器。 – 2013-02-25 19:41:27