2017-05-09 2 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; 
} 

EDIT約CuckooHashTable

typedef struct cuckoo_table CuckooHashTable 
+0

どのラインでエラーが表示されますか? –

+0

'table1 - > slots = malloc(sizeof * table1-> slots)* size);'しかし、私はこの関数で全体的にひどく間違ったことをやっていると思う。 –

+0

変数 'table1'を決して宣言しない。 'table1'は' struct cuckoo_table'のフィールドです。 –

答えて

0

は、私がこのようtable1table2

で物事を指示するために使用されている別のポインタを初期化する必要が判明します。 これが役立つことを願っています。

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

これはテストされていないエラーチェックコードであり、タイプミスeコードがコンパイルされないことがあります。その後、同様の方法でslotsinuseの作業を開始

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

+0

があります。 –

+0

@CookieJarあなたは質問でこれを言及する必要があります。 –

+0

、ありがとう!そして明らかにあなたが示唆した変更の後にまだそこに残っているいくつかの残り物があります。私は 'InnerTable * table1'で元のコードに新しい行を1つ追加しようとしましたが、それ以上のエラーはありません...おそらく私はクリスタルボールを見ています...そしてまた、私の試みはおそらくそれを行う正しい方法ではありません.... –

関連する問題