0
我試圖將鏈接列表引入到我的項目中。我有一般的結構,但我得到兩個錯誤,我不能包裹我的頭。我有「從兼容的指針類型賦值」和「提領指向不完全類型」:從不兼容的指針類型分配和取消引用指針到不完整的類型
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;
^
我用Google搜索周圍,並通過一些過去棧交換的問題看,但一般的問題是使用結構變量聲明人使用後typedef,或者當它引用自身時,不將結構放在成員聲明之前。然而,據我所知,這似乎並不是我的代碼中的情況。 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 }
有誰知道爲什麼這些問題的產生?
謝謝,尼克
at define'ent':'typedef struct {'should be' typedef struct ent {' – BLUEPIXY
} while(map [ce-> y] [ce-> x]!='。'|| entMap [ce - > y] [ce-> x]!= NULL)' - >'while(map [ce-> y] [ce-> x]!='。'&& entMap [ce-> y] [ce- > x]!= NULL)' – BLUEPIXY
@BLUEPIXY謝謝!這工作。但是出於好奇,那麼需要怎麼做呢?另外,很好,趕上,謝謝。 – Nic