-2

//私のテストプログラムは次のとおりです.//基本クラスから継承したデータメンバーを操作したいのですが、「セグメンテーション違反(コアダンプ)」の問題が発生します

#名前空間 std を使用してインクルードします。

class Ui_MainWindow
{
 public:
   int frame;
   int a;
   void setupUI()
    {
     frame = 0;
     cout<<"In setupUI() frame is:"<<frame<<endl;
    }
};


class MainWindow:public Ui_MainWindow
{
 public:
   MainWindow()
    { 
    frame = 111;                //assign for the data member from the base class
    ui->setupUI();
    }
   ~MainWindow(){}
   void paintFrame();
 private:
   MainWindow *ui;
};


void MainWindow::paintFrame()  //I just want to operate the data member which    //inherited from the base class
  {
   frame = 5555;
   cout<<"In MainWindow::paintFrame()frame is:"<<frame<<endl;
  }


int main()
{
 MainWindow w;
 w.paintFrame();
}
4

1 に答える 1

2

uiMainWindowのコンストラクターで初期化されることはありません。メモリ内のランダムな場所を指します。

于 2013-06-27T03:28:32.543 に答える