gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:47:16 25 4
gpt4 key购买 nike

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

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",所以struct client的定义在client.c中是未知的,因此 struct client 在那里表示一个不完整的类型。

关于c - C 中的不完整类型 'struct' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com