-3

I've based this section of my code on the terminal example. When I update a field in my mainwindow I wasn't to store the generated string in a structure, then when a command is called in my secondary source, I want to use the passed string.

In my main.h I have

struct Settings {
        QString Id;
    };Settings settings() const;
private:
    Ui::MainWindow *ui;
    Settings currentSettings;

In my main.cpp is the code that set the value of Id

void MainWindow::on_comboBox_currentTextChanged(const QString &arg1)
{
    QString Data = arg1;
    if (arg1 == "Universal"){
        Data = "xxx";
    }
    else{
        Data = Format_Class::format_3(Data);
    }
    currentSettings.Id = Data;
}

MainWindow::Settings MainWindow::settings() const
{
    return currentSettings;
}

this code defiantly works because currentsettings id gets set to the correct value.

in my mode.H I have

namespace Ui {
class Mode;
}

class MainWindow;
class Mode : public QDialog
{
    Q_OBJECT

public:
    explicit Mode(QWidget *parent = 0);
    ~Mode();

private:
    Ui::Mode *ui;
    MainWindow *settings;
};

then in my mode.cpp, the page that actually uses the data I have

void Mode::on_pushButton_clicked()//ok
{
    MainWindow::Settings p = settings -> settings();
    QString hash = "#";
    QString end = "\r";
    QString identifier = (p.Id);

the function get to the

MainWindow::Settings p = settings -> settings();

at which it develops a fatal error and crashes. In debugging mode when return currentsettings gets called, currentSettings / id is <not accessible> this is after this the program crashes.

when the program crashes the error that occurs is

Signal received

The inferior stopped because it received a signal from the operating system

Signal name: SIGSEGV Signal meaning: Segmentation fault

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jon CO
  • 25
  • 2
  • 10
  • 1
    See [What is a segmantation fault](http://stackoverflow.com/questions/2346806/what-is-segmentation-fault). Your program crashes exactly on a line where you dereference a pointer : make sure you properly initialize that pointer. – Leiaz Aug 04 '15 at 16:25

1 Answers1

0

in Mode, you have:

void Mode::on_pushButton_clicked()//ok
{
    MainWindow::Settings p = settings -> settings();

Are you sure you initialize Mode's settings variable ? It also seems odd to name this variable settings when you have specified its type as MainWindow*.

FredK
  • 4,094
  • 1
  • 9
  • 11