-2

//我的测试程序如下,我只想操作继承自基类的数据成员,但是得到“Segmentation fault (core dumped)”的问题

#include 使用命名空间标准;

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

ui永远不会在MainWindow的构造函数中初始化。它指向内存中的某个随机位置。

于 2013-06-27T03:28:32.543 回答