遇到等于运算符的问题。以退出代码 11 结束

Having trouble with equals operator. Finishing with exit code 11

谁能帮我弄清楚我的 =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:

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供参考:

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;
}

显示的代码中有很多错误。我看到内存泄漏。我看到循环没有正确迭代节点,最终将取消引用 nullptrs。我看到复制构造函数被实现为调用 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();
    }
}

Live Demo

如果您使 virticalList 成为 Nodefriend,那么 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();
    }
}

Live Demo