2015-11-07 88 views
0

以下是我的代碼,用於在鏈接列表中插入數字,如果在頭部包含的數字之後。 的錯誤出現在代碼的最後一行,其中指定節點的指針地址

頭 - >未來= &temp;

的錯誤是:

不能轉換節點**到節點*賦值。

我想要做的就是給head.next指定temp的地址,這樣head就會指向temp。

void LinkedList::insertAfter(int toInsert, int afterWhat) 
{ 
     if(head->data == afterWhat) 
     { 

     Node* temp = new Node; 
     temp->next = head->next; 
     temp->data = toInsert; 
     head->next = &temp; 
     } 
} 
+0

'head-> next = temp;'? – Downvoter

+2

'temp'已經是一個指向節點的指針。你不只是想'head-> next = temp;'? – Kevin

回答

1

因爲temp已經是一個指針了,所以你不必寫& temp。 使用head-> next = temp;

0

你需要

head->next = temp; 

temp類型爲Node*,所以&temp類型是Node**,所以編譯器正確地抱怨。