c++operator-overloadingnodesoperator-keywordequality

Having trouble with equals operator. Finishing with exit code 11


Can somebody help me figure out what is wrong with my =operator? Without these two functions, my program runs perfectly but once they are implemented it results in the error finished with exit code 11.

I am trying to set two linked lists equal to each other.

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

The problem I think is in my operator = function. Or in the private member head.

Here is my full 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;
}

Here is also my node class for reference:

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

Solution

  • The code shown has a lot of mistakes in it. I see memory leaks. I see loops that are not iterating nodes correctly and will eventually dereference nullptrs. I see the copy constructor is implemented to call operator= rather than the other way around.

    I would suggest rewriting the whole thing, for example:

    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

    If you make virticalList be a friend of Node so virticalList can access the Node::next member directly, you can simplify the virticalList constructors a little bit:

    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