|
1Qter豆
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include "QDebug"
- #include "QString"
- struct student
- {
- QString name;
- };
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- QString str = "qqqq";
- student *stu = (student *)malloc(sizeof(student));
- stu->name = str;
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
复制代码 编译通过,但是运行会出错。 stu->name = str;这条语句有问题。 这是为什么
|
最佳答案
查看完整内容
你应该也把报错的那段信息也贴出来,才好判断什么错误。
另外,我不懂为什么你要在c++中用c的内存分配,这是很容易出问题的。你试试把上面的struct改成下面这样:然后下面你的mainwindow构造函数里面改成这样:
|