2017-12-27 130 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; 

     } 

给自己的注意事项:如果他们在船上告诉,不要看关于代码实现的教程。人们可能会忘记东西。