需要帮助解决编译器错误:错误:从“int”转换无效;到“GIO条件”

发布于 2024-08-26 10:24:23 字数 910 浏览 3 评论 0原文

我有一个使用 GIO 的简单 cpp 文件。我已经删除了所有内容以显示我的编译错误:

这是我得到的错误:

My.cpp:16: error: invalid conversion from ‘int’ to ‘GIOCondition’
make[2]: *** [My.o] Error 1

这是完整的文件:

#include <glib.h>

static gboolean
read_socket (GIOChannel *gio, GIOCondition condition, gpointer data)
{
  return false;
}


void createGIOChannel() {
  GIOChannel* gioChannel = g_io_channel_unix_new(0);
  // the following is the line causing the error:
  g_io_add_watch(gioChannel, G_IO_IN|G_IO_HUP, read_socket, NULL);
}

我已经看到使用 gio 的其他示例,并且我在调用 G_IO_IN|G_IO_HUP 方面做了同样的事情。 以及文档 http://www.gtk.org/api /2.6/glib/glib-IO-Channels.html 说我只需要包含 ,我做到了。

您能告诉我如何解决我的错误吗?

我能想到的一件事是我在 cpp 文件中执行此操作。但是g_io_add_watch是ac函数吗?

感谢您的帮助。我花了几个小时在这上面,但没有取得任何进展。

I have a simple cpp file which uses GIO. I have stripped out everything to show my compile error:

Here is the error I get:

My.cpp:16: error: invalid conversion from ‘int’ to ‘GIOCondition’
make[2]: *** [My.o] Error 1

Here is the complete file:

#include <glib.h>

static gboolean
read_socket (GIOChannel *gio, GIOCondition condition, gpointer data)
{
  return false;
}


void createGIOChannel() {
  GIOChannel* gioChannel = g_io_channel_unix_new(0);
  // the following is the line causing the error:
  g_io_add_watch(gioChannel, G_IO_IN|G_IO_HUP, read_socket, NULL);
}

I have seen other example using gio, and I am doing the same thing in term of calling G_IO_IN|G_IO_HUP.
And the documentation http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html, said I only need to include , which I did.

Can you please tell me how to resolve my error?

One thing I can think of is I am doing this in a cpp file. But g_io_add_watch is a c function?

Thank you for any help. I have spent hours on this but did not get anywhere.

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

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

发布评论

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

评论(2

久夏青 2024-09-02 10:24:23

您可以投射它

(GIOCondition) G_IO_IN|G_IO_HUP 

void createGIOChannel() {
  GIOChannel* gioChannel = g_io_channel_unix_new(0);
  // the following is the line causing the error:
  GIOCondition cond = G_IO_IN|G_IO_HUP;
  g_io_add_watch(gioChannel, cond , read_socket, NULL);
}

You can Cast it

(GIOCondition) G_IO_IN|G_IO_HUP 

or

void createGIOChannel() {
  GIOChannel* gioChannel = g_io_channel_unix_new(0);
  // the following is the line causing the error:
  GIOCondition cond = G_IO_IN|G_IO_HUP;
  g_io_add_watch(gioChannel, cond , read_socket, NULL);
}
野稚 2024-09-02 10:24:23

我猜它正在标记一个错误,因为您必须使用执行严格类型检查的 CPP 编译器。

对于 C 编译器,这主要应该是一个警告。

您可以对其进行类型转换 ((GIOCondition) G_IO_IN|G_IO_HUP) 或创建 GIOCondition 类型的变量并将其传递给函数。

I guess it is flagging an error as you must be using a CPP Compiler which does strict typechecking.

Incase of a C compiler, that should mostly be a warning.

You can typecast it ((GIOCondition) G_IO_IN|G_IO_HUP) or create a variable of type GIOCondition and pass the same to the function.

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