【问题标题】:'Table1' undeclared (first use in this function)'Table1' 未声明(在此函数中首次使用)
【发布时间】: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 -&gt; slots = malloc((sizeof *table1-&gt;slots) * size); 但我确实认为我在这个函数中总体上做错了什么......
  • 你永远不会声明变量table1table1struct cuckoo_table 的字段。
  • 使用 cuckoo_table.table1
  • new_cuckoo_hash_table 是否应该为CuckooHashTable 分配内存(大概是struct cuckoo_tabletypedef?)并将-&gt;table1-&gt;table2 设置为为内表?

标签: c eclipse hash hashtable


【解决方案1】:

所以你可能想要这样的东西(只是从 cmets 猜测并看着我的水晶球):

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

这是未经测试的非错误检查代码,可能存在拼写错误,代码可能无法编译。

【讨论】:

  • 在评论中回答您的问题,typedef struct cuckoo_table CuckooHashTable
  • @CookieJar 你应该在问题中提到这一点。
  • 会的,谢谢!显然,在您建议的更改之后,仍然有一些剩菜卡在那里。我确实尝试在我的原始代码中添加一个新行 InnerTable *table1 并且没有更多错误......也许我也在寻找水晶球......而且我也知道我的尝试可能不是正确的方法....
【解决方案2】:

原来我必须初始化另一个指针,用于在 table1table2 处引导事物

像这样:

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

然后以类似的方式开始处理slotsinuse。 希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-05-19
    • 2014-04-22
    • 2012-05-07
    • 2012-07-13
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多