[英]linked list null pointer error c++
我有一個項目要從文件中計算。 由於這是一個學校項目,我不允許使用大多數圖書館,但只能使用基本圖書館。 因此我決定使用哈希映射。 但是我的鏈表給出了一個空指針異常。 准確地說是“拋出異常:讀取訪問沖突。這是 0xCCCCCCCC。發生了”。 我搜索了很多以找到解決方案,但我找不到任何東西。 感謝您的時間。
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;
}
在這里,您創建了一個包含 10000 個列表指針的數組
liste* table[100000];
但你永遠不會為它們創建任何 liste 對象指向。 以后要調用成員函數
table[x]->createnode(word);
由於您在表中的指針仍未初始化,您的呼叫崩潰了。
我的假設是你想讓table成為一個 liste 對象的數組
liste table[100000];
我仍然不明白的是為什么在createNode函數中調用liste() (構造函數?)。
顯然沒有初始化你的類數組會導致這個。添加這個修復了它。
for ( i = 0; i < 100000; i++)
{
table[i] = new liste;
}
自我注意:如果他們在船上講述,請不要觀看有關代碼實現的教程。 人們可能會忘記一些事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.