有人可以帮我找出我的 =operator 出了什么问题吗?如果没有这两个函数,我的程序可以完美运行,但是一旦实现它们,就会导致错误结束,退出代码为 11。
我正在尝试设置两个链表彼此相等。
virticalList::virticalList(const virticalList &p2) {
*this=p2;
}
virticalList & virticalList::operator=(const virticalList& p2)
{
Node* temp = p2.head->getNext();
head = new Node(p2.head->getValue(),0);
Node* curr = head;
while(temp!=NULL){
curr->setNext(new Node());
curr->getNext()->setValue(temp->getValue());
temp = temp->getNext();
}
return *this;
}
我认为问题出在我的operator = 函数中。或者在私人会员头中。
这是我的完整 virticalList 类:
class virticalList
{
private:
Node* head = new Node();
public:
virticalList();
void print();
void virtInc();
~virticalList();
virticalList(const virticalList &p2);
virticalList& operator=(const virticalList& p2);
};
virticalList::virticalList()
{
head -> setValue(10);
Node * ptr = head;
for(int i = 10; i<20; i++)
{
ptr -> setNext(new Node());
ptr -> getNext()->setValue(i);
ptr -> getNext()->setNext(nullptr);
ptr = ptr -> getNext();
}
}
virticalList::~virticalList() {
Node * des = head;
Node * d = des->getNext();
while(des -> getNext()->getValue()!=NULL){
delete des;
des = d;
if(d->getNext()!= nullptr){
d = d->getNext();
}
}
}
void virticalList::print()
{
Node * print = head;
while(print -> getNext()->getValue()!=NULL){
cout << print -> getValue() << " ";
print = print -> getNext();
}
cout << "\n";
}
void virticalList::virtInc()
{
Node * inc = head;
while(inc -> getNext()->getValue()!=NULL){
inc -> setValue(inc -> getValue()+1);
inc = inc -> getNext();
}
}
virticalList::virticalList(const virticalList &p2) {
*this=p2;
}
virticalList & virticalList::operator=(const virticalList& p2)
{
Node* temp = p2.head->getNext();
head = new Node(p2.head->getValue(),0);
Node* curr = head;
while(temp!=NULL){
curr->setNext(new Node());
curr->getNext()->setValue(temp->getValue());
temp = temp->getNext();
}
return *this;
}
这也是我的节点类供参考:
class Node
{
private:
int value;
Node* next;
public:
Node();
Node(int v, Node * next);
void setValue(int v);
int getValue();
Node* getNext();
void setNext(Node* theNewNext);
};
Node::Node()
{
next = 0;
value = 0;
}
Node::Node(int v, Node * next_in)
{
value = v;next = next_in;
}
void Node::setValue(int v)
{
value = v;
}
int Node::getValue()
{
return value;
}
Node* Node::getNext()
{
return next;
}
void Node::setNext(Node* theNewNext)
{
next = theNewNext;
}
显示的代码有很多错误。 我看到内存泄漏。 我看到循环没有正确迭代节点,最终会取消引用
nullptr
。 我看到复制构造函数是为了调用 operator=
而不是相反的方式实现的。
我建议重写整个内容,例如:
class Node
{
private:
int value = 0;
Node* next = nullptr;
public:
Node(int v = 0, Node* n = nullptr);
int getValue() const;
void setValue(int v);
Node* getNext() const;
void setNext(Node* n);
};
Node::Node(int v, Node* n) :
value(v),
next(n)
{
}
int Node::getValue() const
{
return value;
}
void Node::setValue(int v)
{
value = v;
}
Node* Node::getNext() const
{
return next;
}
void Node::setNext(Node* n)
{
next = n;
}
class virticalList
{
private:
Node* head = nullptr;
public:
virticalList();
virticalList(const virticalList &p2);
~virticalList();
virticalList& operator=(const virticalList& p2);
void print() const;
void virtInc();
};
virticalList::virticalList() :
head(new Node(10))
{
Node* ptr = head;
for(int i = 11; i < 20; ++i)
{
ptr->setNext(new Node(i));
ptr = ptr->getNext();
}
}
virticalList::virticalList(const virticalList &p2) {
if (p2.head) {
Node* temp = p2.head;
Node* curr = head = new Node(temp->getValue());
while (temp = temp->getNext()) {
curr->setNext(new Node(temp->getValue()));
curr = curr->getNext();
}
}
}
virticalList::~virticalList() {
Node* ptr = head, *next;
while (ptr) {
next = ptr->getNext();
delete ptr;
ptr = next;
}
}
virticalList& virticalList::operator=(const virticalList& p2)
{
if (this != &p2) {
virticalList temp(p2);
//std::swap(head, temp.head);
Node* ptr = head;
head = temp.head;
temp.head = ptr;
}
return *this;
}
void virticalList::print() const
{
Node* ptr = head;
while (ptr) {
cout << ptr->getValue() << " ";
ptr = ptr->getNext();
}
cout << "\n";
}
void virticalList::virtInc()
{
Node* ptr = head;
while (ptr) {
ptr->setValue(ptr->getValue()+1);
ptr = ptr->getNext();
}
}
如果将
virticalList
设为 friend
的 Node
,这样 virticalList
就可以直接访问 Node::next
成员,则可以稍微简化 virticalList
构造函数:
class Node
{
private:
int value = 0;
Node* next = nullptr;
public:
Node(int v = 0, Node* n = nullptr);
int getValue() const;
void setValue(int v);
Node* getNext() const;
void setNext(Node* n);
friend class virticalList;
};
Node::Node(int v, Node* n) :
value(v),
next(n)
{
}
int Node::getValue() const
{
return value;
}
void Node::setValue(int v)
{
value = v;
}
Node* Node::getNext() const
{
return next;
}
void Node::setNext(Node* n)
{
next = n;
}
class virticalList
{
private:
Node* head = nullptr;
public:
virticalList();
virticalList(const virticalList &p2);
~virticalList();
virticalList& operator=(const virticalList& p2);
void print() const;
void virtInc();
};
virticalList::virticalList()
{
Node** ptr = &head;
for(int i = 10; i < 20; ++i)
{
*ptr = new Node(i);
ptr = &((*ptr)->next);
}
}
virticalList::virticalList(const virticalList &p2) {
Node** curr = &head;
Node* temp = p2.head;
while (temp) {
*curr = new Node(temp->getValue());
curr = &((*curr)->next);
temp = temp->getNext();
}
}
virticalList::~virticalList() {
Node* ptr = head, *next;
while (ptr) {
next = ptr->getNext();
delete ptr;
ptr = next;
}
}
virticalList& virticalList::operator=(const virticalList& p2)
{
if (this != &p2) {
virticalList temp(p2);
//std::swap(head, temp.head);
Node* ptr = head;
head = temp.head;
temp.head = ptr;
}
return *this;
}
void virticalList::print() const
{
Node* ptr = head;
while (ptr) {
cout << ptr->getValue() << " ";
ptr = ptr->getNext();
}
cout << "\n";
}
void virticalList::virtInc()
{
Node* ptr = head;
while (ptr) {
ptr->setValue(ptr->getValue()+1);
ptr = ptr->getNext();
}
}