vector :: push_back< MyClass>不适用于非默认构造函数 [英] vector::push_back <MyClass> not work for non default constructor

查看:148
本文介绍了vector :: push_back< MyClass>不适用于非默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控件类有默认构造函数和复制构造函数和其他构造函数,并赋值运算符,我想使用向量创建我的类的数组。当我调整我的矢量我获得对象初始化正确;但是当我想使用非默认构造函数创建我的对象,我得到这个错误,

i have a Controls class that have default constructor and copy constructor and other constructor ,and an assignment operator , and i want to create array of my class using vector . When i resize my vector i get the objects initialized correctly ; but when i want to create my objects using non default constructor i get this error ,


纯虚方法调用
terminate活动异常

pure virtual method called terminate called without an active exception



Controls.h

class Controls : public QObject
{

private:

    QHBoxLayout Layout ;
    string Controlname;
    std::auto_ptr<QLabel> Label ;
    std::auto_ptr<QSlider> Slider ;
    std::auto_ptr<QSpinBox> Spin ;

public:

    Controls(QLayout &Parent , string name , const int &Default_value);
    Controls(const Controls &copy);
    Controls();
    ~Controls();

    QLabel *const Get_Label()const { return Label.get() ; }
    QSlider *const Get_Slider()const { return Slider.get() ; }
    QSpinBox *const Get_Spin()const { return Spin.get() ; }
    QHBoxLayout *const Get_Layout() {return &Layout;}

    void SetValue(const int &newvalue);

    Controls &operator= (const Controls &copy);


};

Controls.cpp

Controls.cpp

Controls &Controls::operator= (const Controls &copy)
{
    Label = std::auto_ptr<QLabel> ( new QLabel() ) ;
    Slider = std::auto_ptr<QSlider> ( new QSlider() ) ;
    Spin = std::auto_ptr<QSpinBox> ( new QSpinBox() ) ;

    Slider->setValue(copy.Get_Slider()->value());
    Slider->setOrientation(Qt::Horizontal);
    Label->setText(QString ("unamed"));
    Spin->setValue(copy.Get_Spin()->value());


    Layout.addWidget(Label.get() , 0 , 0);
    Layout.addWidget(Slider.get() , 0 , 0);
    Layout.addWidget(Spin.get() , 0 , 0);

    QObject::connect(Slider.get() , SIGNAL(valueChanged(int) ) , Spin.get() , SLOT(setValue(int)));
    QObject::connect(Spin.get() , SIGNAL(valueChanged(int) ) , Slider.get() , SLOT(setValue(int)));


    return *this ;
}
Controls::Controls(const Controls &copy)
{
    *this = copy ;
}
Controls::Controls()
{

    Label = std::auto_ptr<QLabel> ( new QLabel() ) ;
    Slider = std::auto_ptr<QSlider> ( new QSlider() ) ;
    Spin = std::auto_ptr<QSpinBox> ( new QSpinBox() ) ;

    Slider->setValue(0);
    Slider->setOrientation(Qt::Horizontal);
    Label->setText(QString ("unamed"));
    Spin->setValue(0);


    Layout.addWidget(Label.get() , 0 , 0);
    Layout.addWidget(Slider.get() , 0 , 0);
    Layout.addWidget(Spin.get() , 0 , 0);

    QObject::connect(Slider.get() , SIGNAL(valueChanged(int) ) , Spin.get() , SLOT(setValue(int)));
    QObject::connect(Spin.get() , SIGNAL(valueChanged(int) ) , Slider.get() , SLOT(setValue(int)));
}
Controls::Controls(QLayout &Parent , string name , const int &Default_value)
{
    Controlname = name ;

    Label = std::auto_ptr<QLabel> ( new QLabel() ) ;
    Slider = std::auto_ptr<QSlider> ( new QSlider() ) ;
    Spin = std::auto_ptr<QSpinBox> ( new QSpinBox() ) ;

    Slider->setValue(Default_value);
    Slider->setOrientation(Qt::Horizontal);
    Label->setText(QString (name.c_str()));
    Spin->setValue(Default_value);


    Layout.addWidget(Label.get() , 0 , 0);
    Layout.addWidget(Slider.get() , 0 , 0);
    Layout.addWidget(Spin.get() , 0 , 0);

    QObject::connect(Slider.get() , SIGNAL(valueChanged(int) ) , Spin.get() , SLOT(setValue(int)));
    QObject::connect(Spin.get() , SIGNAL(valueChanged(int) ) , Slider.get() , SLOT(setValue(int)));

    Parent.addItem(&Layout);

}

void Controls::SetValue(const int &newvalue)
{
    Slider.get()->setValue(newvalue);
}
Controls::~Controls()
{

}

main.cpp
......

main.cpp ...... .

   vector <Controls> i ;
      i.resize(2 ); // this is work 

     i.push_back(Controls(layout , "WHITE_BALANCE_RED_V" ,12);// this is not working 


推荐答案

这与 std :: vector 无关,
你没有提供整个代码,但我假设这是

This has nothing to do with std::vector and non-default constructors. You did not provide the whole code, but I assume this is the continuation of the previous question.

您的 Controls :: operator = c $ c>无效,它创建 QWidgets 的副本,并将其放入全新的 QLayout 控制对象,你传递给 push_back 是在调用后被销毁的临时对象,它的副本被放入向量, QWidget 成员被放入 QLayout ,它不会被销毁,并且被添加到您尝试显示的小部件(<临时控制对象被销毁后, Panel-> show()调用Panel的 QLayout 方法尝试访问已删除的窗口小部件。

Your Controls::operator= is invalid, it creates copies of QWidgets, and puts them into brand new QLayout. Controls object that you pass to push_back is temporary object that is destroyed after the call and it's copy is put into vector. But destroyed object's QWidget members are put into QLayout, which is not destroyed and which is added to the widget you try to show (Panel). After temporary Controls object is destroyed, Panel->show() calls Panel's QLayout methods that try to access already deleted widgets.

保存向量中控件对象的副本?如果你存储指针,这将摆脱你的问题。为什么你需要这个向量?

Do you really need to save copies of your Controls objects in your vector? If you store pointers, that would get rid of your problem. Why do you need that vector at all?

再次不要使用 auto_ptr ,它已过时,您不需要它来正确管理 QObjects 的删除。

And once again, do not use auto_ptr, it's deprecated and you don't need it to correctly manage deletion of QObjects.

这篇关于vector :: push_back&lt; MyClass&gt;不适用于非默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆