结构的C不完整类型


C incomplete type for struct

本问题已经有最佳答案,请猛点这里访问。

我正在尝试采用RRHEAD,在RRHEAD处设置一个新的RCB点作为下一个点,在新的RCB处设置RRHEAD点作为前一个点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
typedef struct{
  int sequence_number;
  int file_descriptor;
  FILE *requested_file;
  int bytes_remaining;
  int quantum;
  struct RCB *next;
  struct RCB *prior;
} RCB;

typedef struct RCB RCB;


RCB *RRhead = NULL;


static void admit_to_scheduler_RR(int fd, FILE *fin){
int sequence_counter, new_bytes_remaining, new_quantum = 0;
  RCB new_rcb = {sequence_counter, fd, fin, new_bytes_remaining, new_quantum, RRhead, NULL};
  RRhead->prior = &new_rcb;
  RRhead = &new_rcb;
  sequence_counter++;
}

产生以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
sws.c: In function ‘admit_to_scheduler_RR’:
sws.c:318:10: error: variable ‘new_rcb’ has initializer but incomplete type
   struct RCB new_rcb = {sequence_counter, fd, fin, new_bytes_remaining, new_quantum, RRhead, NULL};
          ^
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:10: warning: excess elements in struct initializer [enabled by default]
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default]
sws.c:318:14: error: storage size of ‘new_rcb’ isn’t known
   struct RCB new_rcb = {sequence_counter, fd, fin, new_bytes_remaining, new_quantum, RRhead, NULL};
              ^
sws.c:319:9: error: dereferencing pointer to incomplete type
   RRhead->prior = &new_rcb;
         ^

我不知道为什么我会收到过量元素的警告。我没有正确初始化新的接收器吗?我是否需要创建它,然后将所有字段设置为我想要的?

我相信这一切都是因为新的接收器有一个"不完整的类型"。大多数谷歌搜索都表明这是因为编译器不知道RCB的大小。这似乎表明我需要将这个结构定义放入一个头中。这是绝对必要的吗?


在第一个声明中,您声明RCB是使用typedefstruct。之后,你直接说,‘顺便说一句,RCBstruct RCB的意思,这在这一点上是没有定义的!(要想生存下去,你就得说struct RCB { ... };。)

基本上,删除typedef struct RCB RCB;,消除相关错误,但会弹出新的错误。


尝试如下操作:

1
2
3
typedef struct RCB RCB;
strutc RCB {
 ....

C中的"标记"名称空间(与structunion一起提供)不同于标识符名称空间。通过typedef,还可以将标识符RCB与类型struct RCB关联起来。


尝试将结构更改为以下值(即删除typedef关键字):

1
2
3
4
5
6
7
8
9
struct RCB {
  int sequence_number;
  int file_descriptor;
  FILE *requested_file;
  int bytes_remaining;
  int quantum;
  struct RCB *next;
  struct RCB *prior;
};

在这个答案中,可以读取更多的信息来区分C++中的"结构"和"TyPulfStrut"之间的区别。.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>

typedef struct RCB RCB;

typedef struct RCB {
    int sequence_number;
    int file_descriptor;
    FILE *requested_file;
    int bytes_remaining;
    int quantum;
    RCB *next;
    RCB *prior;
} RCB;

RCB new_rcb = {11, 21, NULL, 31, 41, NULL, NULL};;

static void admit_to_scheduler_RR(int fd, FILE *fin) {

    int sequence_counter = 0;
    int new_bytes_remaining, new_quantum = 0;
    RCB *RRhead = &new_rcb;
    RRhead->prior = &new_rcb;
    sequence_counter++;
}

int main(int argc, char *argv[]) {

    admit_to_scheduler_RR(1, NULL);
    return 0;
}

要修复您的错误,您需要将代码更改为与上面类似的东西,它使用您的数据结构和编译。