Hi everyone
I am relatively new to Qt, and working on game of life. Can someone please help me overcome this error.
ASSERT failure in QVector<T>::operator[]: “index out of range”, file ..\..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include/QtCore/qvector.h, line 356
Here is the code:
dialog.h:
==========================================================================
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include<QtCore>
#include<QtGui>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include"mycell.h"
#include "QTimer"
#include "QVector"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
int gridSize, saveIndex;
bool timeState;
QVector<QVector<bool> > tempGrid;
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_comboBox_activated(int index);
void update_grid();
void on_pushButton_5_clicked();
void on_comboBox_2_activated(int index);
void on_comboBox_3_activated(int index);
private:
Ui::Dialog *ui;
QGraphicsScene *scene;
// myCell *cellArray[100][100];
QVector<QVector<myCell*> > vect;
// myCell *cell;
QTimer *timer;
};
#endif // DIALOG_H
==========================================================================================
dialog.cpp
==============================================================================================
#include "dialog.h"
#include "ui_dialog.h"
#include"mycell.h"
#include "QTimer"
#include "thread.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//tempGrid [gridSize][gridSize];
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
//Dialog.resize(scene->itemsBoundingRect().size());
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(on_pushButton_2_clicked()));
saveIndex = 0;
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked() // Display button
{
scene->clear();
timeState = true;
ui->label->setText(ui->lineEdit->text());
int x, y= 0;
gridSize= 20;
gridSize = ui->label->text().toInt();
ui->graphicsView->resize((gridSize*10)+3,(gridSize*10)+3);
//ui->lineEdit->setText(QString::number(gridSize));
for (int i= 0; i<gridSize; i++){
for(int j=0;j<gridSize;j++){
vect[i][j]= new myCell(i*10,j*10,10);
scene->addItem(vect[i][j]);
//Dialog.resize(scene->addItem(vect[i][j]));
// ui->graphicsView->setSizeIncrement(y,y);
x+=10;
}
x= -100;
y+=10;
}
ui->pushButton->setEnabled(true);
ui->pushButton_2->setEnabled(true);
ui->pushButton_3->setEnabled(true);
// ui->pushButton_4->setEnabled(true);
ui->comboBox->setEnabled(true);
ui->pushButton_5->setEnabled(false);
ui->comboBox_3->setEditable(true);
ui->comboBox_2->setEnabled(true);
on_pushButton_4_clicked();
}
void Dialog::on_pushButton_2_clicked() //step button
{
int getNeighbours = 0;
for (int a=0;a<vect.size();a++)
{
for (int b=0;b<vect.size();b++)
{
tempGrid[a][b] = vect[a][b]->state;
}
}
for (int x=1;x<vect.size()-1;x++)
{
for (int y=1;y<vect.size()-1;y++)
{
getNeighbours = 0;
vect[x][y-1]->state ? getNeighbours +=1:getNeighbours +=0;
vect[x+1][y]->state ? getNeighbours +=1:getNeighbours +=0;
vect[x][y+1]->state ? getNeighbours +=1:getNeighbours +=0;
vect[x-1][y]->state ? getNeighbours +=1:getNeighbours +=0;
vect[x+1][y-1]->state ? getNeighbours +=1:getNeighbours +=0;
vect[x+1][y+1]->state ? getNeighbours +=1:getNeighbours +=0;
vect[x-1][y+1]->state ? getNeighbours +=1:getNeighbours +=0;
vect[x-1][y-1]->state ? getNeighbours +=1:getNeighbours +=0;
tempGrid[x][y]= vect[x][y]->state == true && getNeighbours < 2 ?
false: true;
if (vect[x][y]->state == true && getNeighbours <=3)
{
tempGrid[x][y] = true;
}
else if (vect[x][y]->state == true && getNeighbours > 3)
{
tempGrid[x][y] = false;
}
else if (vect[x][y]->state == false && getNeighbours == 3)
{
tempGrid[x][y] = true;
}
}
}
for (int a=0;a<vect.size();a++)
for (int b=0;b<vect.size();b++)
{
vect[a][b]->state = tempGrid[a][b];
vect[a][b]->update();
}
}
void Dialog::on_pushButton_3_clicked() // Auto Step button
{
timer->start(100);
// ui->pushButton_2->setEnabled(false);
ui->comboBox_3->setEnabled(false);
ui->comboBox->setEnabled(false);
ui->lineEdit->setEnabled(false);
ui->pushButton->setEnabled(false);
ui->pushButton_5->setEnabled(false);
ui->comboBox_2->setEnabled(false);
ui->pushButton_4->setEnabled(true);
}
void Dialog::on_pushButton_4_clicked() //Pause button
{
timer->stop();
ui->pushButton_2->setEnabled(true);
ui->comboBox->setEnabled(true);
ui->lineEdit->setEnabled(true);
ui->pushButton->setEnabled(true);
ui->pushButton_5->setEnabled(true);
ui->comboBox_3->setEnabled(true);
ui->comboBox_2->setEnabled(true);
}
void Dialog::update_grid()
{
for (int a=0;a<gridSize;a++)
for (int b=0;b<gridSize;b++)
vect[a][b]->update();
}
I hope that does not confuse u too much. I don’t know what am I doing wrong!!@@@@
Thanks in advance
↧