为什么我的链表的复制构造函数总是出现分段错误
Why do I keep getting a segmentation fault for my copy constructor function of a linked list?
此函数是链表的复制构造。代码在第一个while循环中的某个地方中止,它只是制作新的链表,不确定是什么原因导致的,任何帮助都会受到赞赏。如果你还需要什么,请告诉我。谢谢
DLL::DLL(DLL& n) {
if (n.headPtr == NULL) {
headPtr = NULL;
}
else {
//keeps track of the last node in the list
Node* lastNode = NULL;
//keeps track of old list
Node* oldListNode = n.headPtr;
while (oldListNode != NULL) {
//create a new Node for new list until reaches end of old list
Node* newNode = new Node;
newNode->pred = NULL;
newNode->succ = NULL;
newNode->ssn = oldListNode->ssn;
newNode->name = oldListNode->name;
//add newNode to new List
if (headPtr == NULL) { //new list is empty
headPtr = newNode;
lastNode = headPtr;
}
else {
//adds new node to the end of the list
lastNode->succ = newNode;
newNode->pred = lastNode;
lastNode = newNode;
}
//Goes to the next node in the old list;
oldListNode = oldListNode->succ;
}
}
//now puts all information from old list into new list
itemCount = n.itemCount;
if (n.headPtr == NULL) {
headPtr = NULL;
}
else {
if (n.itemCount > 0) {
cout << "in while loop, ";
Node *origList = n.headPtr; //pointer accessing old node's data
Node *secondHeadPtr = new Node; //second hptr for new ll
Node *currNode = secondHeadPtr; // points to second ll node that will be filled
Node *nextNode; //going to point to added node
while (currNode != NULL) {
cout << "in while loop, ";
nextNode = new Node; //next node, currnode to point to
currNode->ssn = origList->ssn;
currNode->name = origList->name;
currNode->succ = nextNode; //create beginning of second ll, next node
nextNode->pred = currNode;
currNode = currNode->succ;
origList = origList->succ;
cout << currNode->name << " " << currNode->ssn << " ";
}
}
}
}
头文件
#include <string>
using namespace std;
struct Node {
string ssn;
string name;
Node* succ;
Node* pred;
};
class DLL {
private:
Node* headPtr;
int itemCount;
public:
DLL();
DLL(DLL& n);
virtual ~DLL();
Node* getHeadPtr();
int search(string ss) const;
bool insert(string ss, string name, int & count);
bool remove(string ss, int & count);
int size();
void display();
};
测试文件
#include "DLL.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
DLL myList;
int counter = 0;
int dCounter = 0;
myList.insert("30", "Jack Eblin", counter);
myList.insert("40", "Liz Smith", counter);
myList.insert("10", "Mike Dutter", counter);
myList.insert("20", "Kitty Lekberg", counter);
myList.insert("50", "Carma Meshew", counter);
cout << "After insertion, we should have 10 20 30 40 50 in order" << endl;
myList.display();
cout << "Searching 30 in the list, result should be 2" << endl;
cout << myList.search("30") << endl;
myList.remove("10", dCounter);
myList.remove("50", dCounter);
cout << "After deletion, we should have 20 30 40 in order" << endl;
myList.display();
cout << "Testing copy constructor" << endl;
DLL* temp = new DLL(myList);
cout << "Contents of the original list" << endl;
myList.display();
cout << "Contents of the new list, the memory address of the this list must be different from the
original list" << endl;
if (myList.getHeadPtr() != nullptr) {
temp->display();
}
return 0;
}
在复制构造函数中似乎从未正确设置headPtr
。由于我们在复制构造函数中,在初始化类成员之前,不会初始化它们。这意味着headPtr
可以是任何东西,做任何事情,但设置它的值是未定义的行为。当你到达
if(headPtr == NULL)
headPtr
还没有初始化,所以我们不知道会发生什么。很可能它会有某种不是NULL
的值,如果它是,那么您的副本中的headPtr
将永远不会被设置。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?