error: dereferencing pointer to imcomplete typeC/C++ code// test_hash.c#include stdio.h#include
error: dereferencing pointer to imcomplete type
- C/C++ code
// test_hash.c#include <stdio.h>#include <malloc.h>#include <stdlib.h>#include "hashtable.h"#include "list.h"#include "hashfunction.h"#define N 20int main(){ int i, array[N]; HashTable H; H = InitializeTable(N / 2); srand((unsigned)time(NULL)); for (i = 0; i < N; i++) { array[i] = rand() % 100; printf("array[%d]=%d\n", i, array[i]); Insert(array[i], H); } for (i = 0; i < N / 2; i++) { PrintList(H->TheList[i]); ////////////////// error: dereferencing pointer to imcomplete type } DestroyTable(H); return 0;}//hashtable.h#ifndef _HashSep_H#define _HashSep_H#include "list.h"struct HashTbl;typedef struct HashTbl *HashTable;HashTable InitializeTable(int TableSize);void DestroyTable(HashTable H);Position Find(ElementType Key, HashTable H);void Insert(ElementType Key, HashTable H);ElementType Retrive(Position P);/* Routines such as Delete and MakeEmpty are omitted */#endif //_HashSep_H // hashtable.c#include "hashtable.h"#include "hashfunction.h"#define minsize 3#define ListSize 10struct HashTbl{ int TableSize; List *TheList;};HashTable InitializeTable(int TableSize){ /* HashTable p; int i, j; if (TableSize < minsize) { printf("too small tablesize in hashtable\n"); exit(1); } p = malloc(sizeof(struct HashTbl) * TableSize); if (p == NULL) { printf("memory allocated for HashTable failed\n"); exit(1); } for (i = 0; i < TableSize; i++) { p[i]->TheList = malloc(sizeof(List) * ListSize); if (p[i]->TheList == NULL) { printf("memory allocated for List failed\n"); exit(1); } } for (i = 0; i < TableSize; i++) for (j = 0; j < ListSize; j++) { p[i]->TheList[j]->next = NULL; } return p; */ HashTable H; int i; if (TableSize < minsize) { exit(1); return NULL; } // allocate table H = malloc(sizeof(struct HashTbl)); if (H == NULL) { printf("memory allocated for hashtable failed\n"); exit(1); } // NextPrime ????? // H->TableSize = NextPrime(TableSize); H->TableSize = TableSize; // allocate array of list H->TheList = malloc(sizeof(List) * H->TableSize); if (H->TheList == NULL) { printf("memory allocated for hashtable failed\n"); exit(1); } // allocate list headers for (i = 0; i < H->TableSize; i++) { H->TheList[i] = malloc(sizeof(struct Node)); if (H->TheList[i] == NULL) { printf("memory allocated for hashtable failed\n"); exit(1); } else { printf("%d\t", H->TheList[i]->Element); H->TheList[i]->Next = NULL; } } return H;}void DestroyTable(HashTable H){ int i; for (i = 0; i < H->TableSize; i++) { free(H->TheList[i]); } free(H->TheList); free(H);}Position Find(ElementType Key, HashTable H){ int i; i = hashfunction(Key, H->TableSize); return FindList(Key, H->TheList[i]);}void Insert(ElementType Key, HashTable H){ int i; i = hashfunction(Key, H->TableSize); InsertList(Key, H->TheList[i]);}ElementType Retrive(Position P){ return P->Element;}/* Routines such as Delete and MakeEmpty are omitted */// list.h// linked list with a header// header has the same structure with other struct elements#ifndef _LIST_H #define _LIST_H#include <stdio.h>#include <malloc.h>#include <stdlib.h>struct Node{ int Element; struct Node *Next;};typedef struct Node *PtrToNode;typedef PtrToNode List;typedef List Position;#define ElementType intList MakeEmpty(List L);int IsEmpty(List L);int IsLast(Position P, List L);Position FindList(ElementType X, List L);void Delete(ElementType X, List L);Position FindPrevious(ElementType X, List L);void InsertList(ElementType X, List L);void DeleteList(List L);void PrintList(List L);Position Header(List L);Position First(List L);ElementType Retrieve(Position P);#endif //_LIST_H
程序意图:
利用链表,哈希函数,实现哈希表的 seperate chaining。
这个错误应该怎么解决呢?我在hashtable.h 中已经声明struct HashTlb,在hashtable.c中进行了定义。
在list.h中,对struct Node进行了定义。。都已经存在了,怎么还有错误呢?
[解决办法]
#include "hashfunction.h" 文件呢??
