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 ,
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 ©);
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 ©);
};
Controls.cpp
Controls &Controls::operator= (const Controls ©)
{
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 ©)
{
*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
……
.
vector <Controls> i ;
i.resize(2 ); // this is work
i.push_back(Controls(layout , "WHITE_BALANCE_RED_V" ,12);// this is not working
This has nothing to do with
std::vectorand non-default constructors.You did not provide the whole code, but I assume this is the continuation of the previous question.
Your
Controls::operator=is invalid, it creates copies ofQWidgets, and puts them into brand newQLayout.Controlsobject that you pass topush_backis temporary object that is destroyed after the call and it’s copy is put into vector. But destroyed object’sQWidgetmembers are put intoQLayout, which is not destroyed and which is added to the widget you try to show (Panel). After temporaryControlsobject is destroyed,Panel->show()calls Panel’sQLayoutmethods that try to access already deleted widgets.Do you really need to save copies of your
Controlsobjects in your vector? If you store pointers, that would get rid of your problem. Why do you need that vector at all?And once again, do not use
auto_ptr, it’s deprecated and you don’t need it to correctly manage deletion ofQObjects.