C编译错误:在非结构或联合的情况下请求成员___

时间:2013-05-20 03:02:07

标签: c list compiler-errors linked-list hashtable

我目前正在尝试创建字符串哈希表。但是在我的搜索功能中,我遇到了一个错误:请求成员 _ ,而不是结构或联合......再次

 /*search hash table*/
    ListC search(hash_ref h, char* key){
        ListC* tempList;
        int hashvalue= hashing(h, key);
46      for(tempList= h->List[hashvalue]; tempList!=NULL; tempList=tempList->next){
47          if(strcmp(tempList->key,key)==0){
                return tempList;
            }
        }
        return NULL;
    }

    /*hash function*/
    int hashing(hash_ref h, char* key){
        int hashvalue=0;
        for(hashvalue=0;key!='\0';key++){
            hashvalue= *key + (hashvalue*5) - hashvalue;
        }
        return hashvalue%h->size;
    }

    /*HashTable struct*/
    typedef struct HashTable{
    int size;
    ListC **List;   
    }hash;

    typedef struct Node{
        long key;/*book id*/
        long count;
        struct Node* next;
        struct Node* prev;
    }NodeType;

    typedef NodeType* NodeRef;

    typedef struct ListCount{
        NodeRef first;
        NodeRef last;
        NodeRef current;
        long length;
    }ListCount;

ListC在我的头文件中定义为

typedef struct ListCount* ListC;

在第46和47行,我得到一个错误,说明密钥和下一个是不是结构的成员。我不确定这里的问题是什么

3 个答案:

答案 0 :(得分:2)

typedef struct ListCount* ListC;

所以ListC是指针类型。

ListC* tempList;

tempList是指向ListCount的指针。

... tempList=tempList->next ...

tempList未指向具有名为next的成员的结构。

我建议这说明为什么为指针类型定义typedef通常是个坏主意。无论如何,你必须跟踪间接的水平;如果所有指针类型都是显式的,通常会更容易。

答案 1 :(得分:1)

typedef struct ListCount *ListC;

这一行可能不是你的意思。

  • ListC == struct ListCount *
  • ListC * == struct ListCount **
ListC *foo = whatever;
foo->next;

相当于

struct ListCount *foo = *whatever;
foo.next;

当然不正确。

尽量不要定义指针typedef,这些指针明确表明它们是指针typedef。例如,如果你真的需要,你可以typedef struct ListCount *ListCPtr;或者只是typedef struct ListCount ListC,这就是我认为你想要的。

答案 2 :(得分:0)

ListC是指向指针的指针,指针直接指向结构Listcount.so,* LiatC没有成员next或key。
检查你的typedef定义。