2017-05-09 40 views
2

当编译此代码我收到以下错误..“表1”未申报(在一次使用此功能)

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 
+0

你在哪一行得到错误? –

+0

'table1 - > slots = malloc((sizeof * table1-> slots)* size);'但是我确实认为我在这个函数中做了一些非常糟糕的错误.. –

+0

你永远不会声明一个变量'table1'。 'table1'是'struct cuckoo_table'的字段。 –

回答

0

原来我必须初始化所使用的table1table2

像这种直接的东西另一个指针:

newtable->table = malloc(sizeof (*newtable->table)); 

然后开始以类似的方式对slotsinuse工作。 希望这有助于。

1

所以,你可能想这样的事情(从评论只是猜测和寻找到我的水晶球):

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; 
} 

这是未经测试的,非错误检查代码,可能有错别字和th e代码可能无法编译。

+0

你的问题在注释中,'typedef struct cuckoo_table CuckooHashTable'。 –

+0

@CookieJar你应该在问题中提到这一点。 –

+0

会做,谢谢!显然,在你提出的变化之后,还有一些剩菜残留在那里。我确实尝试添加一行到我的原始代码中,这是'InnerTable * table1',没有更多的错误...也许我也在寻找通过水晶球......而且我也知道一个事实,即我的尝试可能不是正确的做法.... –

相关问题