为什么在这个空对象上迭代会导致核心转储?
我现在正在研究这个迭代程序,当我尝试在空列表上迭代时,我会遇到麻烦。这是感兴趣的守则:
// part A
DoublyLinkedList<int> myListOneNode;
myListOneNode.insertLast(42);
cout << "TestIterationTricky test #1, the next line should display 42" << endl;
stringstream ss;
//see if we can just iterator through one item.
for (auto i : myListOneNode) {
cout << i << " ";
ss << i << " ";
}
cout << endl;
checkTest("TestIterationTricky test #1", "42 ", ss.str()); // is ss equal to "42 " ?
ss.str("");
// part B
DoublyLinkedList<int> myListEmpty;
cout << "TestIterationTricky test #2, the next line shouldn't display anything" << endl;
//see if we can just iterator through one item.
for (auto i : myListEmpty) {
cout << i << " ";
ss << i << " ";
}
cout << endl;
checkTest("TestIterationTricky test #2", "", ss.str()); // is ss equal equal to "" ?
ss.str("");
此摘录中的A部分效果很好,没有错误。
但是,B部分导致分割故障。该程序在达到循环的定义后立即崩溃。
有什么想法吗?
I'm working on this iterating program right now, and I'm running into trouble when I try to iterate over an empty list. Here's the code of interest:
// part A
DoublyLinkedList<int> myListOneNode;
myListOneNode.insertLast(42);
cout << "TestIterationTricky test #1, the next line should display 42" << endl;
stringstream ss;
//see if we can just iterator through one item.
for (auto i : myListOneNode) {
cout << i << " ";
ss << i << " ";
}
cout << endl;
checkTest("TestIterationTricky test #1", "42 ", ss.str()); // is ss equal to "42 " ?
ss.str("");
// part B
DoublyLinkedList<int> myListEmpty;
cout << "TestIterationTricky test #2, the next line shouldn't display anything" << endl;
//see if we can just iterator through one item.
for (auto i : myListEmpty) {
cout << i << " ";
ss << i << " ";
}
cout << endl;
checkTest("TestIterationTricky test #2", "", ss.str()); // is ss equal equal to "" ?
ss.str("");
Part A in this excerpt works fine, no errors.
Part B, however, results in a segmentation fault. The program crashes as soon as it reaches the definition of the for
loop.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论