Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ LinkedList的复制构造函数上的Segfault_C++_Linked List - Fatal编程技术网

C++ LinkedList的复制构造函数上的Segfault

C++ LinkedList的复制构造函数上的Segfault,c++,linked-list,C++,Linked List,我正在编写一个类,该类将用作Student对象的单个链接列表的容器(一个自定义类,其规范与此无关)。它功能完善(我可以轻松添加和删除节点),但复制构造函数除外。使用复制构造函数,我的目标是只复制列表的数据(即不是指针本身),但由于某种原因它会出错 以下是相关代码(用注释说明不明确的部分): 这让我很困惑,因为指针的大小是(而且应该是)8,保存到一个指针(大小是8) 故障的具体原因是什么?为什么这段代码在复制构造函数期间失败,而在调用其他代码时却失败了?我认为这太复杂了,您可以在while循环中执

我正在编写一个类,该类将用作Student对象的单个链接列表的容器(一个自定义类,其规范与此无关)。它功能完善(我可以轻松添加和删除节点),但复制构造函数除外。使用复制构造函数,我的目标是只复制列表的数据(即不是指针本身),但由于某种原因它会出错

以下是相关代码(用注释说明不明确的部分):

这让我很困惑,因为指针的大小是(而且应该是)8,保存到一个指针(大小是8)


故障的具体原因是什么?为什么这段代码在复制构造函数期间失败,而在调用其他代码时却失败了?

我认为这太复杂了,您可以在while循环中执行:

LinkedList::LinkedList(const LinkedList& copy) {
  Student s = copy.head->getStudent();
//Node can be initialized with a Student pointer argument
  head = new Node(new Student(s.getFirstName(),s.getMiddleInitial(),s.getLastName(),s.getSSN(),s.getAge()));
  Node *curr = copy.head->getNext();
  Node *newListCur = head;
  Node* prev = head; //For saving the tail
  while(curr != NULL){
    Student s = curr->getStudent();
   //important step
    Node* newNode = new Node(new Student(s.getFirstName(),s.getMiddleInitial(),s.getLastName(),s.getSSN(),s.getAge()));
    newListCur->setNext(newNode);
   //
    newListCur = newListCur->getNext();
    curr= curr->getNext();
  }
  cout << "Leaving method..." << endl;
}
LinkedList::LinkedList(常量LinkedList©){
学生s=copy.head->getStudent();
//可以使用学生指针参数初始化节点
head=new节点(新学生(s.getFirstName()、s.getMiddleInitial()、s.getLastName()、s.getSSN()、s.getAge());
节点*curr=copy.head->getNext();
节点*newListCur=头;
Node*prev=head;//用于保存尾部
while(curr!=NULL){
学生s=curr->getStudent();
//重要步骤
Node*newNode=新节点(新学生(s.getFirstName(),s.getMiddleInitial(),s.getLastName(),s.getSSN(),s.getAge());
newListCur->setNext(新节点);
//
newListCur=newListCur->getNext();
curr=curr->getNext();
}

难道不是写入的大小无效,而是写入(大小为8)无效。看起来
tail
可能无效(这是传递给
append
的对象)--你确定它正确初始化了吗?你丢失了很多相关的代码。看一看。@Cameron你能解释一下什么会导致写入无效吗?即使只是在一般情况下。@Deduplicator你能详细说明丢失了什么吗?我不认为这是一个问题,我可以简化为一个玩具示例。@ahjohnston25:好的,任何写入都可以无效地址(如保留区域、0等)或程序不拥有的地址无效。可能有空或未初始化的变量。谢谢。此操作运行时不会出现segfaulting。没有问题,欢迎提供帮助:D
==23990== Invalid write of size 8
==23990==    at 0x403018: Node::setNext(Node*) (Node.cpp:86)  //This is the "next = _next" line
==23990==    by 0x402694: LinkedList::append(Node*&) (LinkedList.cpp:81)   
==23990==    by 0x402371: LinkedList::append(Student) (LinkedList.cpp:90)
==23990==    by 0x401F68: LinkedList::LinkedList(LinkedList const&) (LinkedList.cpp:31)
LinkedList::LinkedList(const LinkedList& copy) {
  Student s = copy.head->getStudent();
//Node can be initialized with a Student pointer argument
  head = new Node(new Student(s.getFirstName(),s.getMiddleInitial(),s.getLastName(),s.getSSN(),s.getAge()));
  Node *curr = copy.head->getNext();
  Node *newListCur = head;
  Node* prev = head; //For saving the tail
  while(curr != NULL){
    Student s = curr->getStudent();
   //important step
    Node* newNode = new Node(new Student(s.getFirstName(),s.getMiddleInitial(),s.getLastName(),s.getSSN(),s.getAge()));
    newListCur->setNext(newNode);
   //
    newListCur = newListCur->getNext();
    curr= curr->getNext();
  }
  cout << "Leaving method..." << endl;
}