我在链接列表的深层副本上不断收到分段错误。我在我的“复制构造器”和赋值运算符(运算符=)中使用此深层副本,并得出这样的结论:这是seg错误。返回节点指针的链接列表的深层副本
bigint::Node* bigint::deepcopy(bigint::Node* target){
bigint::Node* current = target;
bigint::Node*cpy = new Node;
cpy->digit = current->digit;
Node* const hd = cpy;
current = current->next;
while(current != nullptr){
bigint::Node* tmp = new Node;
tmp->digit = current->digit;
cpy->next = tmp;
cpy = cpy->next;
current = current->next;
}
return hd;
}
我的节点结构是这样的:
private:
struct Node{
int digit;
Node* next;
};
Node* head;
static Node* deepcopy(Node* target);
我班被关闭,所有的,只是显示的是私下透露与此有关的功能。提前感谢您的任何建议。
http://stackoverflow.com/questions/21476869/constant-pointer-vs-pointer-to-constant – macroland
首先,我会建议开始使用unique_ptr和引用,而不是指针,只要你可以。这通常可以减少您现在最小的错误。深拷贝究竟意味着什么以及您打算如何处理该拷贝? –
@MarošBeťko我认为“深层复制”意味着列表中的所有节点都被复制/克隆。因此,最后你会得到另一个具有相同值的列表 – Thomas