2012-10-18 68 views
0

我有一個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 &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 &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 ...... 。

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

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

我想這只是編輯的問題,但此行是已經不好,應該是「i.push_back(控件(佈局,‘WHITE_BALANCE_RED_V’,12));」 – progician

+0

對不起!我忘了點逗號(我現在編輯它),但我用它在我的真實代碼!並且我得到輸出:當我使用push_back – user1728234

回答

0

這有什麼好做std::vector和非默認的構造函數。 您沒有提供完整的代碼,但我認爲這是previous question的延續。

Controls::operator=是無效的,它創造的QWidgets副本,並將它們放入全新的QLayoutControls您傳遞給push_back的對象是臨時對象,該對象在調用後被銷燬,並且它的副本被放入向量中。但被銷燬的對象的QWidget成員被放入QLayout,該對象未被銷燬,並且被添加到您嘗試顯示的小部件(Panel)中。在臨時對象Controls被銷燬後,Panel->show()調用Panel的QLayout方法試圖訪問已經被刪除的小部件。

你真的需要保存拷貝你的向量中的Controls對象?如果你存儲指針,那將消除你的問題。爲什麼你需要那個矢量?

再一次,使用auto_ptr,它的棄用,你不需要它來正確地管理的QObjects刪除。

+0

確定我改變了我的代碼時,調用了沒有活動異常的稱爲終止的純虛方法! – user1728234

+0

Your Controls :: operator =無效;你能解釋一下巫婆部分是否無效,並且要求你提供 – user1728234

+0

我已經描述 - 在'operator ='中,你爲目的地'Controls'對象創建一個新的佈局,並在那裏放置你的部件的副本。當源控件對象(你從中複製)被銷燬時,窗口小部件也被刪除(感謝'auto_ptr'),但是指向它們的指針存儲在你之前在堆棧上創建的'QVBoxLayout layout'對象中。 'layout'然後被設置到'Panel'中。當你調用Panel-> show()時,它會調用'layout'對象的'QLayout'方法,然後它會嘗試調用已刪除小部件的方法。 – Paul

0

此外,我認爲問題是你的auto_ptr變量。你應該檢查它是否真的如何處理你的對象指針。我寧願堅持shared_ptr或unique_ptr。然而,在後一種情況下,您根本不想擁有複製構​​造函數,因爲只能有一個指針的所有者。

C++ std::auto_ptr copy constructor

0

我不知道你是正確地實現你的拷貝語義,並利用std::auto_ptr作爲數據成員是種「警告標誌」的。

您的Controls是否真的可以複製?

也許你應該只使用scoped_ptr代替auto_ptr數據成員,禁止複製聲明私人拷貝構造函數和私人operator=,並使用vector<shared_ptr<Controls>>

(或者使用C++ 11移動語義,所以使用unique_ptr代替auto_ptr,並且只使用編譯器自動生成的移動操作?)

0

當使用auto_ptr你應該使用releaseget傳遞指針到新東家:

不是這在Controls::Controls(QLayout &Parent , string name , const int &Default_value)

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

但這:

Layout.addWidget(Label.release() , 0 , 0); 

否則 - 你auto_ptr正在刪除此構造函數的範圍末尾處的指針。

相關問題