C中不完整的类型"struct"错误 [英] incomplete type 'struct' error in C

查看:306
本文介绍了C中不完整的类型"struct"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this issue and can't see where the error is so I'm hoping someone can help address it. The error I get from compiling the source is:

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

我在头文件-client.h中有结构定义:

I have the struct definition inside a header file - 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的来源:

And here is the source of 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表示此处的类型不完整.

You have forgotten to #include "client.h", so the definition of struct client is not known in client.c, hence struct client denotes an incomplete type there.

这篇关于C中不完整的类型"struct"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆