【问题标题】:C compiler errors "expected ... before ..." AND invalid type arguementC 编译器错误“预期......之前......”和无效的类型参数
【发布时间】:2011-08-07 12:44:21
【问题描述】:

这是我的第一个有用的 C 程序。我正在实现一个可以采用任何类型的键和项目的通用哈希表。但我遇到了不知道如何解决的错误:
hash.h:32:错误:在“hash_create”之前需要“=”、“,”、“;”、“asm”或“attribute
hash.h:38:错误:“hash_func”之前的预期“)”
编辑: 在将 struct hash_t 类型化为散列后,我收到大量这些错误。
hash.c:12:错误:“->”的类型参数无效(有“哈希”)

#ifndef _HASH_H
#define _HASH_H

/* Definitions for abstract hashtable. */
typedef void * key;
typedef void * item;
typedef void * hash_func;
typedef void * compare_func;

struct pair {
    key k;
    item i;
};

struct hash_t {
    int size;
    int max_size;
    int array[16];
    hash_func hf;
    compare_func cf;
};
typedef int boolean_t;
#ifndef FALSE
#define FALSE 0
#define TRUE (!FALSE)
#endif // FALSE

/*
 * Creates and returns a new hashtable.
 * Returns null on failure, else a valid hashtable.
 */
extern hash_t hash_create();

/*
 * Sets the hash function to the new value if hash is empty.
 * Returns true if set successfully, false if nothing changed.
 */
extern boolean_t hash_set(hash_t, hash_func pf);

/* Sets the function used to compare values.
 * Returns true if set successfully, false if nothing changed.
 */
extern boolean_t hash_compare(hash_t h, hash_func pf);

/*
 * Returns TRUE if hashtable is empty. FALSE otherwise.
 */
extern boolean_t hash_is_empty(hash_t h);

/*
 * Returns number of elements in the table.
 */
extern int hash_size(hash_t h);

/*
 * Adds the key and value pair to hashtable.
 */

extern void hash_add(hash_t h, pair p);

/*
 * Returns the pair associated with the given key.
*/
extern pair hash_lookup(hash_t h, key k);

/*
  * Returns true if key is present, else false.
*/
extern boolean_t hash_is_present(hash_t h, key k);

/*
 * Removes the pair associated with given key.
 * Returns true if successfully removed, else false.
*/
extern boolean_t hash_remove(hash_t h, key k);

#endif //_HASH_H
</pre></code>

In case you want to look at the hash.c as well as the previous hash.h:
<pre><code>
/* Implements hash abstract data type. */ 
#include assert.h> //can't include < in post or it disappears
#include stdio.h> //can't include < in post or it disappears
#include stdlib.h> //can't include < in post or it disappears
#include "hash.h"

hash_t
hash_create()
{
    hash_t h = (hash_t)malloc(sizeof(struct hash_t));
    h->size = 0;
}

boolean_t
hash_set(hash_t, hash_func pf){
    if(!hash_is_empty)
        return FALSE;
    else
        hf = pf;
    return TRUE;
}

boolean_t
hash_set(hash_t, hash_func pf){
    if(!hash_is_empty)
        return FALSE;
    else
        cf = pf;
    return TRUE;
}

boolean_t
hash_is_empty(hash_t h){
    return h->size==0;
}

int
hash_size(hash_t h){
    return h->size;
}

void
hash_add(hash_t h, pair p) {
  int i = h->hash_func(max_size, p->key);
  if(h->array[i]!=NULL)
    i++;
  h->array[i] = p;
  h->size++;
  if(h->size > .75*h->maxsize) {
    h->max_size *= 2;
    int arr[h->max_size];
    int* temp = h->array;
    h->array = arr;
    int i = 0;
    for(i; i < h->max_size / 2; i++) {
      if(temp[i]!=NULL)
    hash_add(h,temp[i]);
    }
}

pair
hash_lookup(hash_t h, key k) {
  int i = h->hash_func(max_size, k);
  while(h->array[i]!=NULL && h->compare_func(h->array[i]->key,k)!=0) {
    i++;
  }
  return h->array[i];
}

boolean_t
hash_is_present(hash_t h, key k) {
  return (hash_lookup(h,k)!=NULL);
}

boolean_t
hash_remove(hash_t h, key k) {
  pair p = hash_lookup(h,k);
  if(p == NULL)
    return FALSE;
  else 
    *p = NULL;
  return TRUE;
}

【问题讨论】:

    标签: c compiler-errors hashtable


    【解决方案1】:

    由于您正在编写 C,因此您需要这样做:

    extern struct hash_t hash_create();
    

    hash_t 不是类型 - struct hash_t 是。这也适用于您使用 hash_t 的所有其他位置 - 请改用 struct hash_t

    或者:

    typedef struct hash_t {
        int size;
        int max_size;
        int array[16];
        hash_func hf;
        compare_func cf;
    } hash;
    
    extern hash hash_create();
    

    【讨论】:

    • 听起来不错!怎么样 typedef struct hash { int size;诠释最大尺寸;整数数组[16];哈希函数高频;比较函数cf; } hash_t;
    • 即,交换 hash_t 和 hash,这样我现有的代码就可以工作了。我猜出于某种原因,这是一种不好的风格?
    • 另外,我在帖子中添加了另一个相关错误。现在第一个已经修复了。
    猜你喜欢
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多