2016-04-21 70 views
-1

我目前正在研究一個學校的哈希表項​​目,我遇到了一個問題,我不知道。我的教授向我們提供了具有我們需要實現的功能的類,並且這些函數使用了模板。模板變量賦值段錯誤11

無論如何,在我的插入函數中,我遇到了一個問題,即在我用來實現散列表的單鏈表結構中設置一個節點的值。

我的問題是這樣的:

void insert(U item1, U item2){ //For my project U is a string 
    Node<U>* temp = headPtr; 

    cout << item1 << endl; //Will print out the string no problem 

    //Assignment attempt 
    temp->SSN = item1; // causes a seg fault 


    temp->name = item2; 
    temp->next = NULL; 
    if(headPtr->next == NULL){ 
     headPtr->next = temp; 
     size++; 
    } 
    else{ 
     Node<U>* temp2 = headPtr; 
     while(temp2->next != NULL){ 
      temp2 = temp2->next; 
     } 
     temp2->next = temp; 
     size++; 
    } 
} 

而且這是相當令人沮喪,因爲在以前的任務我已經能夠正確地使用這個插入功能,它不工作我已得出結論的唯一原因是因爲我必須錯過了我忽略的模板。

而且,這裏是我的node.h文件:

#include <iostream> 
using namespace std; 

template <class T> 
struct Node{ 
T SSN; 
T name; 
Node<T>* next; 
}; 

我試圖分配一個字符串值應該是什麼一個字符串值,並作爲我的理解去,但每次我運行應努力盡可能該計劃它得到了這個地步,也只是部分故障11

+0

是headPtr宣佈爲'節點 *'並且包含調用'新節點' – GMichael

回答

1

必須更換

Node<U>* temp = headPtr; 

Node<U>* temp = new Node<U>; 
+0

的結果這樣做了,謝謝。它是有道理的,我所指的節點是NULL,所以它不能有值。 – Geedubs123