2013-05-29 163 views
0

我想學習glib編程來查看流程,當我使用g_object_new()創建一個對象但在編譯時得到這個錯誤,需要一些提示,我在做什麼錯誤的G_DEFINE_TYPE(第25行) ?GLib程序編譯錯誤

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; 
} 
+0

請幫忙!,需要一些小技巧.. – user2399453

回答

1

它在NetworkListenerClass的typedef一個錯字...有一個大寫字母L,其中不應該有(你有「類」,而不是「類」 )。

+0

謝謝..你一定有鷹眼,你怎麼會發現? :-) – user2399453