我不知道我的移动赋值运算符有什么问题,这里是函数。我不认为我正确抓取数据,因为当我运行测试,我得到一个随机负数和“你的程序已停止工作)移动赋值运算符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