ljuhrich
10th November 2012, 12:22
I wanted to make my application logging, so I added a "QStringList l" to my Mainwindow Class.
Because the Program has some sub-classes that also should be logging, I added Pointers to the mainwindow's QStringList into them.
The program executes the Pledge-Algorithm for a robot to leave a room full of blocks, so I created the Classes "CPledge", "CRob" and "CMap".
The problem: I get a SIGSEGV error when I reference the log-lists of CRob/CMap to the Mainwindow's logList.
CODE:
Mainwindow Class:
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
// !The Log Object!
QStringList l;
void saveLog(QString file);
private:
Ui::MainWindow *ui;
QString i(int i);
QString dt();
private slots:
void on_btStartThread_clicked();
// etc.
};
#endif // MAINWINDOW_H
The CPledge Class (Gets the CRob out of a CMap):
class CPledge : public QObject
{
Q_OBJECT
public:
explicit CPledge(QObject *parent = 0);
CMap *map;
CRob *rob;
void setBot(int x, int y);
// Log list reference
QStringList *l;
private:
void followLeftWallTilTurnIsZero();
public slots:
void solve(QString filePath);
signals:
void finished();
};
The CMap Class (The Labyrinth):
class CMap : public QObject
{
Q_OBJECT
public:
explicit CMap(QObject *parent = 0);
// Log list reference
QStringList *l;
typedef QList<bool> StonesLine;
void loadFromFile(QString FilePath);
bool free(int x, int y);
bool left(int x, int y, int turn);
bool front(int x, int y, int turn);
QPoint movedPos(int x, int y, int turn);
int cleanTurning(int turn);
QPoint *defaultRobPos;
private:
// A 2D bool-list: 1->stone; 0-> No stone
QList <StonesLine> stones;
// The list of points where the rob is free
QPoint freePoints[2];
void insertBlock(int x1, int y1, int x2, int y2);
void initBlocks(int width, int height);
StonesLine horizontalStonesBorder(int width);
StonesLine verticalStonesBorder(int width);
};
The CRob Class (The Robot):
class CRob : public QObject
{
Q_OBJECT
public:
explicit CRob(QObject *parent = 0);
// Log list reference
QStringList *l;
bool free();
bool left();
bool front();
void turnR();
void turnL();
void move();
void setPosition(int newx, int newy);
int turning();
private:
int x, y;
CMap *map;
int fturning;
};
The Error comes here at Mainwindow.cpp:
// etc.
void MainWindow::on_btStartThread_clicked()
{
CPledge *pledge = new CPledge();
pledge->l = &l;
// #########FATAL SIGSEGV ERROR################
pledge->rob->l = &l;
pledge->map->l = &l;
pledge->solve(ui->edMapPath->text());
}
// etc.
Because the Program has some sub-classes that also should be logging, I added Pointers to the mainwindow's QStringList into them.
The program executes the Pledge-Algorithm for a robot to leave a room full of blocks, so I created the Classes "CPledge", "CRob" and "CMap".
The problem: I get a SIGSEGV error when I reference the log-lists of CRob/CMap to the Mainwindow's logList.
CODE:
Mainwindow Class:
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
// !The Log Object!
QStringList l;
void saveLog(QString file);
private:
Ui::MainWindow *ui;
QString i(int i);
QString dt();
private slots:
void on_btStartThread_clicked();
// etc.
};
#endif // MAINWINDOW_H
The CPledge Class (Gets the CRob out of a CMap):
class CPledge : public QObject
{
Q_OBJECT
public:
explicit CPledge(QObject *parent = 0);
CMap *map;
CRob *rob;
void setBot(int x, int y);
// Log list reference
QStringList *l;
private:
void followLeftWallTilTurnIsZero();
public slots:
void solve(QString filePath);
signals:
void finished();
};
The CMap Class (The Labyrinth):
class CMap : public QObject
{
Q_OBJECT
public:
explicit CMap(QObject *parent = 0);
// Log list reference
QStringList *l;
typedef QList<bool> StonesLine;
void loadFromFile(QString FilePath);
bool free(int x, int y);
bool left(int x, int y, int turn);
bool front(int x, int y, int turn);
QPoint movedPos(int x, int y, int turn);
int cleanTurning(int turn);
QPoint *defaultRobPos;
private:
// A 2D bool-list: 1->stone; 0-> No stone
QList <StonesLine> stones;
// The list of points where the rob is free
QPoint freePoints[2];
void insertBlock(int x1, int y1, int x2, int y2);
void initBlocks(int width, int height);
StonesLine horizontalStonesBorder(int width);
StonesLine verticalStonesBorder(int width);
};
The CRob Class (The Robot):
class CRob : public QObject
{
Q_OBJECT
public:
explicit CRob(QObject *parent = 0);
// Log list reference
QStringList *l;
bool free();
bool left();
bool front();
void turnR();
void turnL();
void move();
void setPosition(int newx, int newy);
int turning();
private:
int x, y;
CMap *map;
int fturning;
};
The Error comes here at Mainwindow.cpp:
// etc.
void MainWindow::on_btStartThread_clicked()
{
CPledge *pledge = new CPledge();
pledge->l = &l;
// #########FATAL SIGSEGV ERROR################
pledge->rob->l = &l;
pledge->map->l = &l;
pledge->solve(ui->edMapPath->text());
}
// etc.