C 编译器错误“预期...之前...” AND 类型论证无效
这是我的第一个 C 程序,它做了一些有用的事情。我正在实现一个通用哈希表,它可以采用任何类型的键和项目。但我遇到了错误,我不知道如何修复:
hash.h:32: 错误:在“hash_create”之前应有“=”、“,”、“;”、“asm”或“属性”
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;
}
This is my first C program that does something useful. I'm implementing a generic hash table that can take any type of key and item. But I'm getting errors I don't know how to fix:
hash.h:32: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘hash_create’
hash.h:38: error: expected ‘)’ before ‘hash_func’
EDIT:
Aftertypedeffing struct hash_t to hash, I am getting tons of these errors.
hash.c:12: error: invalid type argument of ‘->’ (have ‘hash’)
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您正在编写C,因此您需要执行以下操作:
hash_t
不是类型 -struct hash_t
是。这也适用于您使用hash_t
的所有其他位置 - 请改用struct hash_t
。或者:
Since you're writing C you need to do:
hash_t
isn't a type -struct hash_t
is. This applies to all other locations where you usehash_t
as well - usestruct hash_t
instead.Alternatively: