简体   繁体   English

指针导致分段错误。 (Qt)

[英]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. 在我的主对话框类中,我声明了一个指向另一个对话框的指针,例如: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. 但是,当我尝试使用例如childDialog-> show()或尝试使用它在两个类之间连接信号和插槽时,程序崩溃。 Anyone know why? 有人知道为什么吗? Did I declare the pointer incorrectly? 我是否错误地声明了指针?

PS I have included the header file in the appropriate places. PS我已经在适当的位置包括头文件。 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 . 只需声明一个指向ChildDialog的指针即可。 No ChildDialog is actually created so when you try to access it, you get a crash. 实际上没有创建任何ChildDialog因此当您尝试访问它时会崩溃。 Rather you need 而是你需要

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

With arguments to the constructor as required. 根据需要为构造函数提供参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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