我有一個Controls
類,它具有默認的構造函數和複製構造函數和其他構造函數,以及賦值運算符,我想使用矢量創建我的類的數組。當我調整我的矢量時,我得到的對象正確初始化;但是,當我想創建使用非默認的構造函數我得到這個錯誤我的對象,vector :: push_back <MyClass>不適用於非默認構造函數
純虛方法稱爲 終止所謂的不存在活躍異常
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
我想這只是編輯的問題,但此行是已經不好,應該是「i.push_back(控件(佈局,‘WHITE_BALANCE_RED_V’,12));」 – progician
對不起!我忘了點逗號(我現在編輯它),但我用它在我的真實代碼!並且我得到輸出:當我使用push_back – user1728234