0

我正在尝试学习 glib 编程以查看使用 g_object_new() 创建对象时的流程,但在编译时出现此错误,需要一些提示,我在 G_DEFINE_TYPE (第 25 行)做错了什么?

gcc -Wall `pkg-config --cflags --libs glib-2.0` loop.c -o loop
loop.c: In function ‘network_listener_get_type’:
loop.c:25: error: invalid application of ‘sizeof’ to incomplete type ‘NetworkListenerClass’ 
loop.c: In function ‘main’:
loop.c:60: warning: unused variable ‘nl’

代码是:

#include <stdio.h>
#include <glib.h>
#include <glib-object.h>

#define NETWORK_LISTENER_TYPE   (network_listener_get_type())
#define NETWORK_LISTENER(obj)   (G_TYPE_CHECK_INSTANCE_CAST ((obj), NETWORK_LISTENER_TYPE, NetworkListener))

typedef struct _NetworkListener NetworkListener;
typedef struct _NetworkListenerCLass NetworkListenerClass;


 struct _NetworkListener
 {
   GObject parent_instance;
   unsigned int ipAddr;
   int port;
 };

 struct _NetworkListenerClass
 {
   GObjectClass parent_class;
 };

 G_DEFINE_TYPE(NetworkListener, network_listener, G_TYPE_OBJECT);

 static GObject * network_listener_constructor(GType gtype,
                              guint nprops,
                              GObjectConstructParam *props)
 {
    GObject *obj;
    printf("network listener constructor called\n");
    {
      obj = G_OBJECT_CLASS(network_listener_parent_class)->constructor(gtype, nprops, props);
    }
    return obj;
 }

 static void network_listener_class_init(NetworkListenerClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
   printf("network listener class init called\n");
   gobject_class->constructor = network_listener_constructor;
 }

 static void network_listener_init(NetworkListener *self)
 {
   printf("Called network_listener_init\n");
                                                                    1,28          Top
 }

 gboolean callback(gpointer data)
 {
   printf("%s\n", (char*)data);
   return TRUE;
 }

 int main()
 {
   GMainLoop* main_loop = NULL;
   NetworkListener *nl = g_object_new(NETWORK_LISTENER_TYPE, NULL);
   main_loop = g_main_loop_new (NULL, FALSE);
/* interval, function callback, userdata */
   g_timeout_add_seconds(10, callback, "callback_function");
   g_main_loop_run(main_loop);
   return 0;
 }
4

1 回答 1

1

这是 NetworkListenerClass typedef 中的一个错字......有一个大写的 L 不应该是(你有“CLass”而不是“Class”)。

于 2013-05-30T00:49:24.417 回答