2012-12-24 36 views
2

我有这个问题,看不到错误在哪里,所以我希望有人可以帮助解决它。我从编译源得到的错误是:C中的不完整类型'struct'错误

client.c:15:54: error: invalid application of ‘sizeof’ to incomplete type ‘struct client’ 

我有一个头文件中的结构定义 - client.h:

#ifndef PW_CLIENT 
#define PW_CLIENT 

#include <event2/listener.h> 
#include <event2/bufferevent.h> 
#include <event2/buffer.h> 

#include <arpa/inet.h> 

#include <stdlib.h> 
#include <stdio.h> 
#include <errno.h> 

struct client { 
    int id; 
    struct bufferevent *bev; 

    struct client *prev; 
    struct client *next; 
}; 

struct client *client_head = NULL; 

struct client* client_connect(struct bufferevent *bev); 
#endif 

这里是client.c来源:

#include <event2/listener.h> 
#include <event2/bufferevent.h> 
#include <event2/buffer.h> 

#include <arpa/inet.h> 

#include <stdlib.h> 
#include <stdio.h> 
#include <errno.h> 

struct client* client_connect(struct bufferevent *bev) { 
    // Client info 
    struct client *c = (struct client*)malloc(sizeof(struct client)); 
    if (c == NULL) { 
     // error allocating memory 
    } else { 
    if (client_head == NULL) { 
     // initialize list addresses 
     c->prev = c->next = NULL; 

     // set connection id 
     c->id = 0; 
    } else { 
     // set list addresses 
     client_head->next = c; 
     c->prev = client_head; 
     c->next = NULL; 
     client_head = c; 

     // set connection id 
     c->id = (c->prev->id + 1); 
    } 

     // initialize user vars 
     c->bev = bev; 
    } 

    return c; 
} 

谢谢!

+2

为什么包含来自头文件和C文件的相同头文件? –

+0

无论如何它们中的大部分都不应该包括在内,这是在尝试通过错误2小时后尝试进行的braindead。 :/ – user1667175

回答

5

您忘记了#include "client.h",因此struct client的定义在client.c中未知,因此struct client表示此处不完整。

+0

我在另一个.c文件中,不知道我必须将它包含在client.c中。已经试过包括它之前,编译器仍然给我一个错误,但将它放在client.c再编译好o_O谢谢! – user1667175

2

很抱歉,但你需要包括client.h,编译器只编译什么,他被告知...

0

我没有看到

#include "client.h" 

在.c文件

相关问题