繁体   English   中英

C中的不完整类型'struct'错误

[英]incomplete type 'struct' error in C

我有这个问题,无法看到错误在哪里,所以我希望有人可以帮助解决它。 我从编译源代码得到的错误是:

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

谢谢!

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

对不起,但你需要包含client.h,编译器只编译他被告知的内容......

我没有看到

#include "client.h"

在.c文件中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM