2012-12-24 35 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文件

相關問題