2017-12-27 133 views
-1

我有一個項目從一個文件計數。由於這是一個學校項目,我不能使用大多數的圖書館,但基本的圖書館。因此我決定使用哈希映射。但我的鏈表給出了一個空指針異常。準確地說是「異常拋出:讀取訪問衝突 這個是0xCCCCCCCC發生」。我搜索了很多找到解決方案,但我找不到任何東西。感謝您的時間。鏈接列表空指針錯誤C++

public: liste() { 
     head = NULL; 
     tail = NULL; 
    } 

    void createnode(string value) { 
     liste(); 
     node* temp = new node; 
     temp->data = value; 
     temp->next = NULL; 

     if (head == NULL) { // get error here!!!! 

      head = temp; 
      tail = temp; 
      temp = NULL; 
     } 

     else { 
      tail->next = temp; 
      temp = tail; 
     } 
    } 

     int main() 
    { 
     struct site { 
      int count; 
      string name; 
     }; 
     unsigned long int i=0; 
     unsigned int x=0; 
     ifstream theFile("access_log"); 
     string word; 
     liste* table[100000]; 
     site list[10]; 

     while (theFile >> word) { 
      if (word.find(".")!=-1) { 
       if (word.find("H") == -1) { 
        x=(int)hashG(word); 
        if (x < 100000) {     
         table[x]->createnode(word); 
        } 
       } 
      } 

     } 
     for (int i = 0; i < 100000; i++) { 
      cout << table[i]->length() << " " << table[i]->getData() << endl; 
     } 
     return 0; 
    } 
+0

這是一個常規錯誤,指示您的程序觸發了一些未定義的行爲。這可能意味着你在某處有指針錯誤 - 寫入內存不應該。 (另外,你的程序不完整。) – metal

+1

'liste * table [100000];'然後'table [x] - > createnode(word);'。你在哪裏分配/初始化'table [x]'? –

+0

哦,我沒有意識到,我需要分配/初始化它。我看過的所有教程都沒有。 – Redout

回答

1

在這裏,你創建的清單當然10000三分

liste* table[100000]; 

一個數組,但你永遠不會創建任何對象清單當然他們指向。 後來你要調用的成員函數

table[x]->createnode(word); 

如表指針你仍然沒有初始化您的通話崩潰。

我的假設是,你想擁有是清單當然對象的數組

liste table[100000]; 

我仍然不明白的是,爲什麼你叫清單當然()(構造函數?)在你的createNode函數中。

+0

這是我很抱歉的初始化的藉口。至於其他你是對的。 – Redout

0

顯然不是初始化你的類數組導致this.adding這個固定它。

for (i = 0; i < 100000; i++) 
     { 
      table[i] = new liste; 

     } 

給自己的注意事項:如果他們在船上告訴,不要看關於代碼實現的教程。人們可能會忘記東西。