Я пытался ввести связанные списки в свой проект. У меня общая структура вниз, но я получаю две ошибки, из-за которых я не могу окутать голову. У меня есть «Присвоение от несовместимого типа указателя» и «разыменование указатель к неполному типу»:Назначение от несовместимого указателя и разыменование указателя на неполный тип
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 перед объявлением участника, когда он ссылается на себя. Однако, насколько я могу судить, это не похоже на мой код. 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 }
Кто-нибудь знает, почему эти проблемы возникают?
Спасибо, Nic
at define 'ent':' typedef struct {'должно быть' typedef struct ent {' – BLUEPIXY
BTW' 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