我正在尝试为图表编写一个类Node。每个节点都由:
表示我编写了以下三个函数:
Node(const Node &);
Node & operator=(const Node & );
~Node();
我将父节点分配给现有节点的那一刻。在运行时,我得到一个很大的例外。好东西,我没有炸毁电脑。我能正确编码以下三个函数吗?
我删除了距离和id的getter和setter函数,以缩短代码。
谢谢。#ifndef NODE_H
#define NODE_H
class Node{
public:
Node(int);
//rule of three
Node(const Node &);
Node & operator=(const Node & );
~Node();
//getters and setters
int getId() const;
int getDistance() const;
Node* getParent() const;
void setParent(Node *);
void setDistance(int);
private:
int _distance;
int _id;
Node * _parent;
};
#endif
#include "node.h"
Node::Node(int id):_id(id),_distance(INT_MAX),_parent(nullptr){}
Node::Node(const Node & other):_distance(other.getDistance()),_parent(nullptr){
cout << "copy constructor " << endl;
_parent = new Node(other.getId());
if(other.getParent() != nullptr){
_parent->setParent(other.getParent());
}
}
Node::~Node(){
cout <<"destructor" << endl;
if(_parent != nullptr)
delete _parent;
}
Node & Node::operator=(const Node& other){
cout << "assignment " << endl;
//self reference
if(this == &other){
return *this;
}
if(other.getParent() != nullptr){
_parent->setParent(other.getParent());
}
_id = other.getId();
_distance = other.getDistance();
return *this;
}
Node* Node::getParent() const{
return _parent;
}
int Node::getId() const{
return _id;
}
void Node::setParent(Node * parent){
_parent = parent;
}
主要的一切顺利:
Node n0(0);
Node n1(1);
Node n2(2);
Node n3(3);
// constructor(n0);
Node n4 = n0;
cout << n4.getParent() << endl;
cout << n0.getParent() << endl;
直到我这样做:
====&GT; n3.setParent(安培; N4);&LT; == 即可。在运行时,我得到以下内容:
*** Error in `./graph': free(): invalid pointer: 0x00007ffecf36b250 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fd4b50637e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7fd4b506be0a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fd4b506f98c]
./graph[0x401007]
./graph[0x406128]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fd4b500c830]
./graph[0x400df9]
======= Memory map: ========
00400000-0040a000 r-xp 00000000 08:08 1442084 /home/hani/Documents/dataStructurecplusplus/graph/graph
00609000-0060a000 r--p 00009000 08:08 1442084 /home/hani/Documents/dataStructurecplusplus/graph/graph
0060a000-0060b000 rw-p 0000a000 08:08 1442084 /home/hani/Documents/dataStructurecplusplus/graph/graph
009f1000-00a23000 rw-p 00000000 00:00 0 [heap]
7fd4b0000000-7fd4b0021000 rw-p 00000000 00:00 0
7fd4b0021000-7fd4b4000000 ---p 00000000 00:00 0
7fd4b4cdc000-7fd4b4de4000 r-xp 00000000 08:08 3932346 /lib/x86_64-linux-gnu/libm-2.23.so
7fd4b4de4000-7fd4b4fe3000 ---p 00108000 08:08 3932346 /lib/x86_64-linux-gnu/libm-2.23.so
7fd4b4fe3000-7fd4b4fe4000 r--p 00107000 08:08 3932346 /lib/x86_64-linux-gnu/libm-2.23.so
7fd4b4fe4000-7fd4b4fe5000 rw-p 00108000 08:08 3932346 /lib/x86_64-linux-gnu/libm-2.23.so
7fd4b4fec000-7fd4b51ab000 r-xp 00000000 08:08 3932341 /lib/x86_64-linux-gnu/libc-2.23.so
7fd4b51ab000-7fd4b53ab000 ---p 001bf000 08:08 3932341 /lib/x86_64-linux-gnu/libc-2.23.so
7fd4b53ab000-7fd4b53af000 r--p 001bf000 08:08 3932341 /lib/x86_64-linux-gnu/libc-2.23.so
7fd4b53af000-7fd4b53b1000 rw-p 001c3000 08:08 3932341 /lib/x86_64-linux-gnu/libc-2.23.so
7fd4b53b1000-7fd4b53b5000 rw-p 00000000 00:00 0
7fd4b53bc000-7fd4b53d2000 r-xp 00000000 08:08 3936718 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd4b53d2000-7fd4b55d1000 ---p 00016000 08:08 3936718 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd4b55d1000-7fd4b55d2000 rw-p 00015000 08:08 3936718 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd4b55d4000-7fd4b5746000 r-xp 00000000 08:08 7211138 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fd4b5746000-7fd4b5946000 ---p 00172000 08:08 7211138 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fd4b5946000-7fd4b5950000 r--p 00172000 08:08 7211138 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fd4b5950000-7fd4b5952000 rw-p 0017c000 08:08 7211138 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fd4b5952000-7fd4b5956000 rw-p 00000000 00:00 0
7fd4b595c000-7fd4b5982000 r-xp 00000000 08:08 3932330 /lib/x86_64-linux-gnu/ld-2.23.so
7fd4b5b80000-7fd4b5b81000 rw-p 00000000 00:00 0
7fd4b5b81000-7fd4b5b82000 r--p 00025000 08:08 3932330 /lib/x86_64-linux-gnu/ld-2.23.so
7fd4b5b82000-7fd4b5b83000 rw-p 00026000 08:08 3932330 /lib/x86_64-linux-gnu/ld-2.23.so
7fd4b5b83000-7fd4b5b85000 rw-p 00000000 00:00 0
7fd4b5b85000-7fd4b5b8b000 rw-p 00000000 00:00 0
7ffecf34c000-7ffecf36d000 rw-p 00000000 00:00 0 [stack]
7ffecf3bc000-7ffecf3be000 r--p 00000000 00:00 0 [vvar]
7ffecf3be000-7ffecf3c0000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
答案 0 :(得分:0)
问题不是由n3.setParent(&amp; n4)引起的;你可以通过添加{char c; cin&gt;&gt; c;}在此语句之后,以及在main()结束之前。
问题是由if(_parent!= nullptr)删除_parent;
引起的节点n0-n4在main()的堆栈上创建,因此当main()结束时将被删除。但是,其中一个节点已经被析构函数中的delete语句消失了,它会给你free():无效指针:0x00007ffecf36b250错误。
对使用new。
创建的对象使用delete