Click here to Skip to main content
14,868,514 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

```
C++
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!=0){
        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:
```
C++
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 = 11; 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();
    for (int i = 0; i<10; i++){
        delete des;
        des = d;
        if(d->getNext()!= nullptr){
            d = d->getNext();
        }
    }
}

void virticalList::print()
{
     Node * print = head;
     for (int i = 0; i<10; i++){
         cout << print -> getValue() << " ";
         print = print -> getNext();
     }
     cout << "\n";
}

void virticalList::virtInc()
{
    Node * inc = head;
    for (int i = 0; i<10; i++){
        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!=0){
        curr->setNext(new Node());
        curr->getNext()->setValue(temp->getValue());
        temp = temp->getNext();
    }
    return *this;
}

```
Here is also my node class for reference:
```
C++
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;
}

```

What I have tried:

I have tried changing the self-assignment operator
Posted
Updated 25-Nov-20 13:59pm
v2
Comments
honey the codewitch 25-Nov-20 19:34pm
   
I'll have a crack at this after I've had some sleep but what really jumped out at me was it's spelled "vertical" not "virtical" and I'm having a hard time getting past that.

1 solution

I am surprised that compiles. I didn't think members could be initialized like that. I don't think I would ever do it like that though. I would initialize that to nullptr and assign its value in a method, possibly the constructor.

We can't tell you why your program exits with 11 because we can not see the code that you are running. We see some code for node and the other class but nothing that has an instance of the class.

BTW - it is a really, really bad idea to use literal values like 10, 11, and 20. I see no reason those should even be there. You should traverse lists until they end, not for a predetermined number. If you want to put 10 values in then loop from 0 to 10. The value to be saved can be based on the loop index but don't index the loop based on those values. Also - the word is spelled vertical.

I think you should make an add method and you should use that instead of what you are doing. Here is what that might look like with a list that acts like a stack.
C++
class VerticalList
{
public:
    VerticalList()   {}

    void AddValue( int value )
    {
        // create a node.  It's next item used to be the head of the list

        Node * pnode = new Node( value, m_phead );

        // the new item is now the head of the list

        m_phead = pnode;   
    }

private:
    Node * m_phead = nullptr;
};
To remove an item you link around the item being removed meaning the node that had the one being removed as its next, now has the removed item's next item as its next item. That means you need to traverse the list and find that item to maintain the list's links. For example if your list is a->b->c->null when you remove b then the list is a->c->null. Item b was the next item for a so a's next is now c.

To copy a list linked like this, you have to first find the tail of the list and add the items in reverse order so the head item is added last.

To put ten values in the list, starting with 11 you can do this :
C++
VerticalList vl;
for( int n = 0; n < 10; ++n )
{
    vl.AddValue( n + 11 );
}
If you copy a list by traversing from head to tail it will be in reverse order from the original. If you copy it again then it will be back to its original order since it would have been reversed twice.
   
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900