头文件中的一些struct-typedef声明似乎没有进入我的实现文件,这让我遇到了一些问题。
hashCode
函数来补充我正在设计的引用所有这些数据类型的哈希表。类型和值可以正常工作,但Integer/Float/String不能正常工作,我不知道为什么。
我将从hashCode函数开始(请不要批评它有多糟糕,我知道它不太好,我真的不在乎):
int hashCode(ST_HashSymbol *hash, Value *v)
{
Type *t = v->type;
switch (whichType(t->name))
{
case INTEGER:
Integer *i = (Integer *)v->innerValue;
return i->value % hash->capacity;
case FLOAT:
{
Float *f = (Float *)v->innerValue;
float val = f->value;
long l = 0l;
if (val 2 && j = 0; --j)
{
if (val >= pow(2, j - 22))
{
val -= pow(2, j - 22);
l |= 1 capacity;
}
case STRING:
String *s = (String *)v->innerValue;
char *str = s->value;
int total = 0;
char *c;
for (c = str; *c != '\0'; ++c)
{
total += *c;
}
return total % hash->capacity;
default:
return -1;
}
}
摘录自“type.h”头文件,它定义了所有类型。值得注意的是,我也尝试将typedef和struct定义合并为一个语句,但这也不起作用:
typedef struct _t Type;
typedef struct _v Value;
struct _t {
char *name;
struct _t *widerType;
};
struct _v {
Type *type;
void *innerValue;
};
Type *type(int);
int whichType(char *);
Type *getType(char *);
/**************************/
/* Actual ("inner") types */
/**************************/
typedef struct _str String;
typedef struct _int Integer;
typedef struct _fl Float;
struct _str {
int length;
char *value;
};
struct _int {
int value;
};
struct _fl {
float value;
};
当我运行make时,我得到以下信息:
[kparting@dhcp-10-25-247-130 eq]$ make
gcc -o eq -Wall -g parser.c eq.c error.c hash.c symbols.c type.c -lm
hash.c: In function âhashCodeâ:
hash.c:33: error: expected expression before âIntegerâ
hash.c:34: error: âiâ undeclared (first use in this function)
hash.c:34: error: (Each undeclared identifier is reported only once
hash.c:34: error: for each function it appears in.)
hash.c:37: error: expected expression before âFloatâ
hash.c:38: error: âfâ undeclared (first use in this function)
hash.c:69: error: expected expression before âStringâ
hash.c:70: error: âsâ undeclared (first use in this function)
make: *** [eq] Error 1
正如我所提到的,Type*和Value*是有效的数据类型,但其他三种不是。这个
whichType
type
函数不使用其他三种数据类型中的任何一种。