我正在嘗試實現鏈接列表,並使用某些方法來補充鏈接列表。 然而,一旦我複製鏈接列表,並刪除第一個鏈表的頭,並實現我的重載運算符「< <」輸出鏈表,我得到一個錯誤鏈接列表的複製構造函數的正確實現
Unhandled exception at 0x0128506C in program.exe: 0xC0000005: Access violation reading location 0xFEEEFEF2.
你怎麼複製鏈表不依賴於第一個鏈表。
NodeSLList list2 (list1); //copy constructor
temp = list1.DeleteFromHead();
cout << "node retrieved " << temp.data << endl;
cout << "cout << list1 " << endl;
cout << list1 << endl;
cout << list2 << endl; //error occurs
拷貝構造函數
NodeSLList::NodeSLList(NodeSLList & list)
{
head = list.head;
tail = list.tail;
cout << "copy constructor called" << endl;
}
鏈接列表複製構造函數應該創建鏈接列表的副本。只是製作指針值的副本不會這樣做。您需要在副本中重新創建整個鏈接列表。 – PaulMcKenzie 2014-11-14 23:49:09