【问题标题】:incomplete type 'struct' error in CC中的不完整类型“结构”错误
【发布时间】:2012-12-24 15:53:01
【问题描述】:

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

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

谢谢!

【问题讨论】:

  • 为什么你在头文件和 C 文件中包含相同的头文件?
  • 无论如何,它们中的大多数都不应该包含在内,在尝试解决错误 2 小时后,这是一种脑残的尝试。 ://

标签: c pointers struct


【解决方案1】:

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

【讨论】:

  • 我在另一个 .c 文件中有它,不知道我也必须将它包含在 client.c 中。之前尝试过包含它,但编译器仍然给我一个错误,但是在将它再次放入 client.c 后它编译得很好 o_O 谢谢!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我没看到

    #include "client.h"
    

    在您的 .c 文件中

    【讨论】:

      猜你喜欢
      • 2015-02-06
      • 2022-01-09
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多