【发布时间】:2017-05-09 15:37:29
【问题描述】:
编译此代码时,我收到以下错误..
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
【问题讨论】:
-
你在哪一行得到错误?
-
table1 -> slots = malloc((sizeof *table1->slots) * size);但我确实认为我在这个函数中总体上做错了什么...... -
你永远不会声明变量
table1。table1是struct cuckoo_table的字段。 -
使用 cuckoo_table.table1
-
new_cuckoo_hash_table是否应该为CuckooHashTable分配内存(大概是struct cuckoo_table的typedef?)并将->table1和->table2设置为为内表?