Qt创建者中的c ++空队列初始化不为空

时间:2016-02-07 20:45:14

标签: c++ qt

因此队列不为空并且填充了随机数量的元素。这可能也是我尝试将元素推送到此队列时程序崩溃的原因。

calculator.cpp中的cerr语句的输出:

0
12163576194217602005

calculator.cpp:

void Calculator::insertZero(){
  cerr << input.empty();
  cerr << input.size();
  //input.push(0.00);
}

calculator.hpp:

#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <QString>
#include "widget.h"
#include "ui_widget.h"
#include <queue>
#include <iostream>

/*
 * This class holds all declarations of the calculator.
 */


using namespace std;

class Calculator{

private:
    queue<double> input;
    queue<double> result;
    QString display;

public:

    //numbers
    void insertZero();

};

#endif // CALCULATOR_H

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QString>
#include <QPushButton>
#include <QLabel>
#include "calculator.hpp"

namespace Ui {
class Widget;
}

class Calculator; //forward declaration

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    Calculator *calc;

private slots:
    void on_zero_clicked();


private:
    Ui::Widget *ui;

};

#endif // WIDGET_H

widget.cpp:

#include "ui_widget.h"
#include "widget.h"
#include "calculator.hpp"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

}

Widget::~Widget()
{
    delete ui;
}

//BUTTON FUNCTIONS
void Widget::on_zero_clicked()
{
    calc -> insertZero();
    ui -> label ->setText("0");
}

1 个答案:

答案 0 :(得分:1)

问题是您没有在calc的构造函数中初始化成员Widget()

要解决此问题,请将构造函数更改为:

Widget::Widget(QWidget *parent) :
  QWidget(parent),
  ui(new Ui::Widget),
  calc(new Calculator)
{
  ui->setupUi(this);
}

请记住在析构函数中删除calc