c++ empty queue initialization isn't empty in Qt creator -


so queue not empty , there random number of elements filled in. reason why program crashes if try push element onto queue.

output cerr statements in calculator.cpp:

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>    /*   * class holds declarations of 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");  }

the problem not initializing member calc in constructor of widget().

to fix this, change constructor to:

widget::widget(qwidget *parent) :   qwidget(parent),   ui(new ui::widget),   calc(new calculator) {   ui->setupui(this); } 

and remember delete calc in destructor


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -