代码之家  ›  专栏  ›  技术社区  ›  Platinum Azure

结构的typedef似乎没有在头文件中通过?

  •  0
  • Platinum Azure  · 技术社区  · 17 年前

    头文件中的一些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 函数不使用其他三种数据类型中的任何一种。

    2 回复  |  直到 17 年前
        1
  •  2
  •   caf    17 年前

    在C语言中,只能在块的开头声明变量。您的线路:

    Integer *i = (Integer *)v->innerValue;
    

    试图声明一个变量i,但它是 不 在街区的起点。只需打开一个块即可解决此问题:

    case INTEGER:
    {
        Integer *i = (Integer *)v->innerValue;
        return i->value % hash->capacity;
    }
    

    case

        2
  •  4
  •   Community Mohan Dere    9 年前

    不能在case块内声明变量。

    那不是 完全 事实上是这样。 See here. 应该能帮你把事情弄清楚。