C++ GTK+;:无法初始化GtkWidget*数据成员
我试图在构造函数中的类中分配一个C++ GTK+;:无法初始化GtkWidget*数据成员,c++,gtk,C++,Gtk,我试图在构造函数中的类中分配一个GtkWidget指针,但是遇到了运行时错误 #include <gtk/gtk.h> class MainWindowController { private: GtkWidget * appWindow; const gchar * windowTitle = "Window title"; public: MainWindowController(GtkApplication * app); ~MainWindowControl
GtkWidget
指针,但是遇到了运行时错误
#include <gtk/gtk.h>
class MainWindowController
{
private:
GtkWidget * appWindow;
const gchar * windowTitle = "Window title";
public:
MainWindowController(GtkApplication * app);
~MainWindowController();
void show();
};
MainWindowController::MainWindowController(GtkApplication* app)
{
//this works
GtkWidget* window = gtk_application_window_new(app);
//not this
//appWindow = gtk_application_window_new(app);
/*adding it into the init list does not work either... I have tried
MainWindowController::MainWindowController(GtkApplication* app)
:appWindow(gtk_application_window_new(app)) {}
*/
gtk_window_set_title(GTK_WINDOW(appWindow), windowTitle);
gtk_window_set_default_size(GTK_WINDOW(appWindow), 500, 500);
}
为什么这不会导致错误
GtkWidget* window = gtk_application_window_new(app);
但这是真的吗
appWindow = gtk_application_window_new(app);
更新:这是我的main.cpp
#include <gtk/gtk.h>
#include "mainWindowController.hpp"
static void
activate (GtkApplication* app,
gpointer user_data)
{
MainWindowController mainController(app);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), 0, 0);
g_object_unref (app);
return status;
}
#包括
#包括“mainWindowController.hpp”
静态空隙
激活(GTK应用程序*应用程序,
gpointer用户(U数据)
{
主窗口控制器主控制器(应用程序);
}
int
主(内部argc,
字符**argv)
{
GTK应用程序*app;
智力状态;
app=gtk_application_new(“org.gtk.example”,G_application_FLAGS_NONE);
g_信号连接(应用程序,“激活”,g_回调(激活),空);
状态=g_应用程序运行(g_应用程序(应用程序),0,0);
g_object_unref(应用程序);
返回状态;
}
我还使用:g++
pkg-config--cflags gtk+-3.0
-o main.cpppkg-config--libs gtk+-3.0
-std=c++0x进行编译您没有显示完整的代码,但我认为问题在于这一行:
MainWindowController mainController(app);
在这里,您创建了一个类型为MainWindowController
的局部变量mainController
,然后该变量立即被销毁,因为它超出了范围
现在,接下来发生什么的细节取决于这个类的析构函数做什么。我猜您正在对appWindow做一些事情,使对象无效(
gtk\u widget\u destroy()
可能?)。或者,您可能试图在调用析构函数后在信号中使用appWindow
,这是未定义的行为。您没有显示完整的代码,但我认为问题出在这一行:
MainWindowController mainController(app);
在这里,您创建了一个类型为MainWindowController
的局部变量mainController
,然后该变量立即被销毁,因为它超出了范围
现在,接下来发生什么的细节取决于这个类的析构函数做什么。我猜您正在对appWindow做一些事情,使对象无效(
gtk\u widget\u destroy()
可能?)。或者,您可能试图在调用析构函数后在信号中使用appWindow
,这是未定义的行为。我们需要查看更多代码。您发布的内容似乎没有问题。我们需要查看更多代码。你贴的东西似乎没什么问题。注意!是我的析构函数,我正在现场调用g_object_unref(appWindow)!是我的析构函数,我调用g_object_unref(appWindow)