错误:无效值没有被忽略,因为它应该是 - C/GTK+

发布于 2024-11-02 13:13:45 字数 2048 浏览 2 评论 0原文

我正在尝试在 GTK+ 中创建一个窗口,其选项卡如下所示:

在此处输入图像描述

每当用户单击“新建”按钮,出现一个新选项卡。

然而,当我编译我的程序时,我得到了一个错误:无效值没有被忽略,因为它应该是

phongcao@phongcao:~$ g++ /home/phongcao/C++/GTK+/newtab.cc -o /home/phongcao/C++/GTK+/newtab `pkg-config gtk+-2.0 --cflags --libs`
/home/phongcao/C++/GTK+/newtab.cc: In function ‘int main(int, char**)’:
/home/phongcao/C++/GTK+/newtab.cc:51:3: error: void value not ignored as it ought to be


这是代码:

#include <gtk/gtk.h>

void destroy(GtkWidget *window, gpointer data) {
  gtk_main_quit();
}

void new_tab(GtkNotebook *notebook, GtkWidget *content, GtkWidget *hbox) {
  gtk_notebook_append_page(notebook, content, hbox);
}

int main(int argc, char *argv[]) 
{
  GtkWidget *window, *label, *content;
  GtkWidget *button, *hbox, *notebook;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  gtk_window_set_title(GTK_WINDOW(window), "New Tab");
  gtk_widget_set_size_request(window, 300, 200);

  notebook = gtk_notebook_new();

  button = gtk_button_new_with_label("New");
  label = gtk_label_new("Tab");
  hbox = gtk_hbox_new(FALSE, 5);
  content = gtk_label_new("This is a tab");
  gint a = 0;

  gtk_box_pack_start_defaults(GTK_BOX(hbox), label);
  gtk_box_pack_start_defaults(GTK_BOX(hbox), button);

  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), content, hbox);
  gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);

  //This following line is where the error is from:
  g_signal_connect(G_OBJECT(button), "new_tab", G_CALLBACK(new_tab(GTK_NOTEBOOK(notebook), content, hbox)), NULL); 

  g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);

  gtk_container_add(GTK_CONTAINER(window), notebook);

  gtk_widget_show_all(hbox); 
  gtk_widget_show_all(window);

  gtk_main();
  return 0;
}


感谢您的帮助!!我真的很感激!!

I am trying to create a window in GTK+ that has tab looks like this:

enter image description here

Whenever the user clicks in the "New" button, there is a new tab appear.

However, when I was compiling my program I got an error: void value not ignored as it ought to be

phongcao@phongcao:~$ g++ /home/phongcao/C++/GTK+/newtab.cc -o /home/phongcao/C++/GTK+/newtab `pkg-config gtk+-2.0 --cflags --libs`
/home/phongcao/C++/GTK+/newtab.cc: In function ‘int main(int, char**)’:
/home/phongcao/C++/GTK+/newtab.cc:51:3: error: void value not ignored as it ought to be

And here is the code:

#include <gtk/gtk.h>

void destroy(GtkWidget *window, gpointer data) {
  gtk_main_quit();
}

void new_tab(GtkNotebook *notebook, GtkWidget *content, GtkWidget *hbox) {
  gtk_notebook_append_page(notebook, content, hbox);
}

int main(int argc, char *argv[]) 
{
  GtkWidget *window, *label, *content;
  GtkWidget *button, *hbox, *notebook;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  gtk_window_set_title(GTK_WINDOW(window), "New Tab");
  gtk_widget_set_size_request(window, 300, 200);

  notebook = gtk_notebook_new();

  button = gtk_button_new_with_label("New");
  label = gtk_label_new("Tab");
  hbox = gtk_hbox_new(FALSE, 5);
  content = gtk_label_new("This is a tab");
  gint a = 0;

  gtk_box_pack_start_defaults(GTK_BOX(hbox), label);
  gtk_box_pack_start_defaults(GTK_BOX(hbox), button);

  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), content, hbox);
  gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);

  //This following line is where the error is from:
  g_signal_connect(G_OBJECT(button), "new_tab", G_CALLBACK(new_tab(GTK_NOTEBOOK(notebook), content, hbox)), NULL); 

  g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);

  gtk_container_add(GTK_CONTAINER(window), notebook);

  gtk_widget_show_all(hbox); 
  gtk_widget_show_all(window);

  gtk_main();
  return 0;
}

Thanks for your help!! I really appreciate it!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在巴黎塔顶看东京樱花 2024-11-09 13:13:45

行号对我来说没有多大意义,但我认为你的问题就在这里(出于说明目的而重新格式化):

g_signal_connect(G_OBJECT(button), "new_tab",
    G_CALLBACK(new_tab(GTK_NOTEBOOK(notebook), content, hbox)), /* <== Badness */
    NULL
);

你正在调用 G_CALLBACK 内的 new_tab 函数宏。 new_tab 函数返回 void (即无返回值),但它是在需要值的上下文中调用的,因此会出现“void value notignored”错误。我认为你的意思是这样说的:

g_signal_connect(G_OBJECT(button), "new_tab", G_CALLBACK(new_tab), NULL);

The line number doesn't make much sense to me but I think your problem is right here (reformatted for illustrative purposes):

g_signal_connect(G_OBJECT(button), "new_tab",
    G_CALLBACK(new_tab(GTK_NOTEBOOK(notebook), content, hbox)), /* <== Badness */
    NULL
);

You're calling the new_tab function inside the G_CALLBACK macro. The new_tab function returns void (i.e. no return value) but it is being called in a context that needs a value and hence the "void value not ignored" error. I think you mean to say something more like this:

g_signal_connect(G_OBJECT(button), "new_tab", G_CALLBACK(new_tab), NULL);
怎会甘心 2024-11-09 13:13:45
g_signal_connect(G_OBJECT(button), "new_tab", G_CALLBACK(new_tab(GTK_NOTEBOOK(notebook), content, hbox)), NULL);

您确定要将参数传递给 new_tab() 吗?简单地说就是G_CALLBACK(new_tab)。这将解决错误。

如果您想调用“new_tab”,那么您必须在适当的位置显式调用该函数(例如在传递给G_CALLBACK之前)。

对于您的疑问,您可以参考这个小教程

g_signal_connect(G_OBJECT(button), "new_tab", G_CALLBACK(new_tab(GTK_NOTEBOOK(notebook), content, hbox)), NULL);

Are you sure you want to pass parameters to new_tab() ? Simply put G_CALLBACK(new_tab). That will resolve error.

If you want to call "new_tab" then you have to call the function explicitly at proper place (say before passing to G_CALLBACK).

For your query you can refer to this small tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文