c - 从不兼容指针类型进行赋值以及取消引用指针到不完整类型

标签 c pointers linked-list

我试图将链表引入我的项目中。我已经掌握了总体结构,但是我遇到了两个我无法理解的错误。我有“从不兼容的指针类型赋值”和“取消引用不完整类型的指针”:

ents.c: In function ‘prepend’:
ents.c:13:30: warning: assignment from incompatible pointer type
             list->head->prev = node;
                              ^
ents.c:14:24: warning: assignment from incompatible pointer type
             node->next = list->head;
                        ^
ents.c: In function ‘removeEntLink’:
ents.c:22:15: error: dereferencing pointer to incomplete type
     link->prev->next  = link->next;
               ^
ents.c:23:15: error: dereferencing pointer to incomplete type
     link->next->prev = link->prev;

ents.c:53:22: warning: assignment from incompatible pointer type
                 node = node->next;
                      ^

我用谷歌搜索并浏览了一些过去的堆栈交换问题,但通常问题是人们在使用 typedef 之后使用 struct 进行变量声明,或者当它引用自身时没有将 struct 放在成员声明之前。然而,据我所知,我的代码中似乎并非如此。 ents.h:

1 #ifndef H_ENTS
2 #define H_ENTS
3 #include <stdlib.h>
4 #include <curses.h>
5 #include "map.h"
6 typedef struct {
7     int y, x, hp;
8     char symbol;
9     struct ent *next;
10     struct ent *prev;
11 }ent;
12 
13 typedef struct {
14     int size;
15     ent *head;
16     ent *tail;
17 } entList;
18 
19 #define ENTS 6
20 extern entList *eList;
21 extern ent *entMap[mapY][mapX];
22 void prepend();
23 void removeEntLink();
24 void initEnt();
25 void drawEnts();
26  
27 #endif

和 ents.c:

1 #include "ents.h"
2 #include "map.h"
3 
4 entList *eList;
5 ent *entMap[mapY][mapX];
6 void prepend(entList *list, ent *node) {
7     if(list) {
8         if(!list->head) {
9             list->head = node;
10             list->tail = node;
11         }   
12         else {
13             list->head->prev = node;
14             node->next = list->head;
15             list->head = node;
16         }   
17         node->prev = NULL;
18         list->size++;
19     }   
20 }   
21 void removeEntLink(ent *link) {
22     link->prev->next  = link->next;
23     link->next->prev = link->prev;
24     link = NULL;
25 }   
26 void initEnt() {
27     eList->size = 0;
28     while(eList->size < ENTS) {
29         ent *ce;
30         do {
31             ce->y = rand() % mapY;
32             ce->x = rand() % mapX;
33         } while (map[ce->y][ce->x] != '.' || entMap[ce->y][ce->x] !=  NULL );
34         if(eList->size == 0) {
35             ce->symbol = '@';
36             ce->hp=10; 
37         }   
38         else {
39             ce->symbol = 'k';
40             ce->hp=3;
41         }   
42         entMap[ce->y][ce->x]=ce;
43         prepend(eList, ce);
44     }   
45 }   
46 // Draw entities
47 void drawEnts() {
48     if(eList) {
49         if(elist->head) {
50             ent *node = elist->head;
51             while(node != NULL) {
52                 mvaddch(node.y, node.x, node.symbol);
53                 node = node->next;
54             }   
55         }   
56     }   
57 
58 }

有谁知道为什么会出现这些问题?

谢谢,尼克

最佳答案

你写了:

struct ent *next;

但是,您的程序不包含名为 struct ent 的类型。 (它确实有一个 ent 类型,但这不是同一回事)。

您可以更改第 6 行以调用类型 struct ent:

typedef 结构体 { ... } ent;

- 请注意,这使得同一类型的 struct entent 成为两个不同的名称。

关于c - 从不兼容指针类型进行赋值以及取消引用指针到不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854162/

相关文章:

从数组中复制字符

c - 在 C 中标记字符串

c++ - 在运行时区分指针和引用 ANSI C++

c++ - 由于指针导致的段错误

c - 链表插入行为

c - 删除c中链表中最后一项的问题

c - 在链表中的另一个节点之前插入一个节点时遇到问题?

C: 使用 strcpy 将字符串分配给结构数组导致的段错误

c - 静态库加载两次

c++ - :bad_alloc in passing vector reference to pointer to vector in constructor