编译此代码时,我收到以下错误。。
tables/cuckoo.c: In function 'new_cuckoo_hash_table':
tables/cuckoo.c:35:9: error: invalid type argument of '->' (have 'CuckooHashTable')
table1 -> slots = malloc((sizeof *table1->slots) * size);
^
tables/cuckoo.c:35:42: error: invalid type argument of '->' (have 'CuckooHashTable')
table1 -> slots = malloc((sizeof *table1->slots) * size);
^
In file included from tables/cuckoo.c:11:0:
tables/cuckoo.c:36:15: error: invalid type argument of '->' (have 'CuckooHashTable')
assert(table1->slots);
^
tables/cuckoo.c:37:8: error: invalid type argument of '->' (have 'CuckooHashTable')
table1->inuse = malloc((sizeof *table1->inuse) * size);
^
tables/cuckoo.c:37:40: error: invalid type argument of '->' (have 'CuckooHashTable')
table1->inuse = malloc((sizeof *table1->inuse) * size);
^
In file included from tables/cuckoo.c:11:0:
tables/cuckoo.c:38:18: error: invalid type argument of '->' (have 'CuckooHashTable')
assert(table1->inuse);
我认为错误并没有就此停止,所有
new_cuckoo_hash_table
中的变量都可能处理不当。。。我知道这与我没有为我的
table1
声明类型有关,但让我困惑的是,我有一个包含InnerTable *table1
的结构,希望有人能指出这个错误的原因。如有任何更正,将不胜感激!
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "cuckoo.h"
typedef struct inner_table {
int64 *slots; // array of slots holding keys
bool *inuse; // is this slot in use or not?
} InnerTable;
// a cuckoo hash table stores its keys in two inner tables
struct cuckoo_table {
InnerTable *table1; // first table
InnerTable *table2; // second table
int size; // size of each table
};
// initialise a cuckoo hash table with 'size' slots in each table
CuckooHashTable *new_cuckoo_hash_table(int size) {
InnerTable table1;
assert(size < MAX_TABLE_SIZE && "error: table has grown too large!");
table1 -> slots = malloc((sizeof *table1->slots) * size);
assert(table1->slots);
table1->inuse = malloc((sizeof *table1->inuse) * size);
assert(table1->inuse);
return NULL;
//return NULL;
}
编辑:
给那些需要更多关于
CuckooHashTable
typedef struct cuckoo_table CuckooHashTable
最佳答案
所以你可能想要这样的东西(只是从评论中猜测,然后看看我的水晶球):
CuckooHashTable *new_cuckoo_hash_table(int size) {
assert(size < MAX_TABLE_SIZE && "error: table has grown too large!");
CuckooHashTable *newtable = malloc(sizeof(CuckooHashTable));
assert(newtable);
newtable->table1 = malloc(sizeof(InnerTable));
assert(newtable->table1);
newtable->table1->slots = malloc((sizeof *table1->slots) * size);
assert(table1->slots);
newtable->table1->inuse = malloc((sizeof *table1->inuse) * size);
assert(table1->inuse);
return newtable;
}
这是未经测试的非错误检查代码,可能存在拼写错误,并且代码可能无法编译。