2013-05-20 66 views
-2

我目前正在嘗試創建一個字符串哈希表。然而,在我的搜索功能,我已經運行到了一個錯誤:爲會員要求_的東西不是一個結構或聯合..再次C編譯錯誤:請求成員___的東西不是結構或工會

/*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行,我得到一個錯誤,說明關鍵字和下一個是不是結構的成員。我不知道這裏有什麼問題

+0

「listC」的定義是什麼? –

+0

你如何定義struct ListC? – Amadeus

+0

你'tempList'的類型是'指向ListC'的指針,但是你沒有展示'ListC'是如何定義的。現在,你的'Node'類型是唯一一個似乎定義了'next'字段的類型。 –

回答

2
typedef struct ListCount* ListC; 

所以ListC是一個指針類型。

ListC* tempList; 

tempList是一個指針的指針到一個ListCount

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

tempList不指向具有一個名爲next成員的結構。

我認爲這說明了爲什麼定義指針類型typedef通常是一個壞主意。無論如何你必須跟蹤間接的程度;如果所有指針類型都是顯式的,那麼通常會更容易。

+0

那麼什麼是更容易解決問題的方法呢? – user2388648

1
typedef struct ListCount *ListC; 

此行可能不是您的意思。

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

相當於

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

這當然是不正確。

儘量不要使指針類型定義變得明顯,它們是指針typedefs。例如,如果您真的需要,您可以typedef struct ListCount *ListCPtr;或者只是typedef struct ListCount ListC,這是我認爲你想要的。

+0

事情是如果我將其更改爲typedef struct ListCount ListC並仍然產生相同的錯誤。我甚至將我的散列結構中的ListC ** List更改爲ListC * List,並且仍然產生相同的錯誤 – user2388648

0

ListC是一個指向指針的指針,它直接指向結構Listcount.so,* LiatC沒有成員下一個或鍵。
檢查您的typedef定義。

相關問題