未初始化的引用成员'Widget :: handler'

时间:2014-04-30 09:17:25

标签: c++ qt

此错误;

C:\Users\Seb\Desktop\SDIcw2\widget.cpp:6: error: uninitialized reference member 'Widget::handler' [-fpermissive]
 Widget::Widget(QWidget *parent) :
 ^
在我为组合框添加事件处理程序后发生

。有谁知道为什么?小部件运行正常,所以我不确定为什么!

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include <QString>

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

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

void Widget::getList(SDI::shipHandler& shipHandler)
{
    handler = shipHandler;
}

void Widget::populateCombo()
{
    ui->comboBox->addItem("Select Vessel...");
    for(std::vector<SDI::navalVessels*>::const_iterator i = handler.ships.begin(); i != handler.ships.end(); ++i)
    {
        SDI::navalVessels* ship = *i;
        QString qstr = QString::fromStdString(ship->name);
        ui->comboBox->addItem(qstr);
    }
}



void Widget::on_comboBox_currentIndexChanged(int index)
{
    for(std::vector<SDI::navalVessels*>::const_iterator i = handler.ships.begin(); i != handler.ships.end(); ++i)
    {
        SDI::navalVessels* ship = *i;
        QString qstr = QString::fromStdString(ship->name);
        QString str = ui->comboBox->currentText();
        if (str == qstr)
        {
            //do something here
        }
    }

}

尝试根据组合框中选择的内容更改框的值。

1 个答案:

答案 0 :(得分:0)

您尚未展示widget.h,但可能您有一个类似于:

的私有部分
private:
    Ui::Widget *ui;
    ShipHandler *handler;

您的编译器提醒您未初始化的指针可能很危险,因此您需要确保更改构造函数以初始化它们:

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