简体   繁体   中英

Pointer causes segmentation fault. (Qt)

I'm writing a program which has two dialogs. In my main dialog class I declared a pointer to the other dialog like such: ChildDialog *childDialog. However, when I try to use it for example childDialog->show() or try to use it to connect signals and slots between the two classes, my program crashes. Anyone know why? Did I declare the pointer incorrectly?

PS I have included the header file in the appropriate places. So I don't think that's the problem. Thanks in advance!

You need to allocate the object--

ChildDialog *childDialog;

Just declares a pointer to a ChildDialog . No ChildDialog is actually created so when you try to access it, you get a crash. Rather you need

ChildDialog* childDialog = new ChildDialog(...);

With arguments to the constructor as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM