我不知道我的移動賦值運算符有什麼問題,這裏是函數。我不認爲我正確抓取數據,因爲當我運行測試,我得到一個隨機負數和「你的程序已停止工作)移動賦值運算符C++
virtual LinkedList<T> &operator=(LinkedList<T> &&other)
{
cout << " [x] Move *assignment* operator called. " << endl;
// Delete our own elements
ListNode<T> *temp1 = _front;
while (temp1 != nullptr)
{
ListNode<T> *n = temp1->getNext();
delete temp1;
temp1 = n;
}
// Grab other data for ourselves
ListNode<T> *temp2 = other._front;
while (temp2 != nullptr)
{
addElement(temp2->getValue());
temp2 = temp2->getNext();
}
// Reset their pointers to nullptr
other._front = nullptr;
other._end = nullptr;
other._size = 0;
other._last_accessed_index = 0;
other._last_accessed_node = nullptr;
return *this;
}
測試代碼 - 這是我的老師測試代碼 -
// Use move *assignment* operator
cout << " [x] Test #5: Move *assignment* constructor behavior" << endl;
moved1 = LinkedList<int>{ 6, 7, 8, 9, 10 };
cout << " [x] Result:" << endl;
cout << " [x] Expected:\t6 7 8 9 10" << endl;
cout << " [x] Actual:\t\t";
for (int i = 0; i < moved1.getSize(); i++)
{
cout << moved1.getElementAt(i) << " ";
}
cout << endl << endl;
這是我第一次與移動和移動賦值運算符的工作謝謝:)
當您逐步完成代碼時,調試器會顯示什麼內容? –
它會運行所有的東西,但是當我運行我的測試代碼時,它試圖接收列表中的數據時會中斷@KenWhite –
您能發佈測試代碼 - 或者最好是它的最小子集,它可以演示問題嗎? – norlesh