我試圖實現一個哈希表。我得到了上面的錯誤,但AFAIK我已經正確地聲明瞭所有的結構體/ typedefs。 (我仍然在學習C,對不起) 錯誤發生在我的while循環中的test.c中,我把//錯誤。怎麼了?「取消引用指向不完整類型的指針」,typedefs可以
test.c的
#include "mentry.h"
#include "mlist.h"
int main() {
FILE *fPtr = fopen("S.txt", "r");
MList *ml = ml_create();
MEntry *m2;
int i = 1;
while ((m2 = me_get(fPtr)) != NULL) {
unsigned long int hash = me_hash(m2, ml->numbuckets); // ERROR HERE
i++;
}
return 0;
}
mlist.c
#include "mentry.h"
#include "mlist.h"
#define NUMBUCKETS 3
#define BUCKETSIZE 5
typedef struct bucket {
int size; // number of elements in this bucket
MEntry *entries; // array of MEntries
} Bucket;
typedef struct mlist {
int numbuckets; // number of buckets
Bucket **buckets; // an array of Bucket pointers
} MList;
/* ml_create - creates a new mailing list
- returns pointer to start of mailing list*/
MList *ml_create(void) {
MList *ml = malloc(sizeof(MList));
ml->numbuckets = NUMBUCKETS; // random initial number
// will increment once a bucket is full
// init Buckets
int b;
for (b=0; b<ml->numbuckets; b++) {
Bucket *bk = malloc(sizeof(Bucket));
bk->size = 0;
bk->entries = calloc(BUCKETSIZE, sizeof(MEntry));
ml->buckets[b] = bk;
}
return ml;
}
mlist.h
#ifndef _MLIST_H_
#define _MLIST_H_
#include "mentry.h"
typedef struct mlist MList;
/* ml_create - creates a new mailing list
- returns pointer to start of mailing list*/
MList *ml_create(void);
#endif /* _MLIST_H_ */
在'test.c'有numbuckets'的'沒有明顯的界定。您需要將'mlist.c'中的結構定義移動到標題中 –