0
//the copy constructor. Make sure this performs deep copy!!!!
IntList::IntList(const IntList& cpy){
    IntNode* tmp = new IntNode;
    head = 0; 
    tail = 0;

    for(IntNode* i = cpy.head; i != 0; i = i->next){
        push_back(i->data);
    }
}

these are error message

*** Error in `./a.out': double free or corruption (fasttop): 0x0000000001fd3280 ***
Test feedback
Passed empty list test
Passed single node test
Passed 2+ node test

and below is overloaded operator

IntList& IntList::operator=(const IntList& rhs){
    while(head != 0){
        IntNode *tmp = head;
        head = head -> next;
        delete tmp;
    }
    tail = head;

    for(IntNode* i = rhs.head; i != 0; i = i -> next){
        push_back(i->data);
    }

    return *this;
}

this is error message for overloaded operator

*** Error in `./a.out': double free or corruption (fasttop): 0x0000000001c4b280 ***
Test feedback
Copy constructor did not work with self-assignment (i.e., list1 = list1).
Expected IntList: -1 40 136 144 -100 121 101 42 -23 -31 199
Actual IntList: 

these are my copy constructor, and overloaded assignment operator.

I understand the concept of deep copy, but i'm confused to implement.

Can anyone help?

and my push_back is this

void IntList::push_back(int value){
    if(empty() == true){
        head = tail = new IntNode(value);
        head ->next = tail;
    }
    else if(empty() == false){
        IntNode* tmp = new IntNode(value);
        tail->next = tmp;
        tmp->next = 0;
        tail = tmp;
    }
}
Lincoln Oh
  • 13
  • 3
  • And what exactly is the problem? Is it code not working / not compiling or something else? What is the question? – thorhunter Nov 12 '16 at 12:28
  • my error is this Test copy constructor. Your output *** Error in `./a.out': double free or corruption (fasttop): 0x0000000001fd3280 *** Test feedback Passed empty list test Passed single node test Passed 2+ node test Test operator= function. This test uses operator<< function. Your output *** Error in `./a.out': double free or corruption (fasttop): 0x0000000001c4b280 *** Test feedback Copy constructor did not work with self-assignment (i.e., list1 = list1). Expected IntList: -1 40 136 144 -100 121 101 42 -23 -31 199 Actual IntList: – Lincoln Oh Nov 12 '16 at 22:08
  • You may want to check this then: http://stackoverflow.com/questions/5517698/check-for-self-assignment-in-copy-constructor – thorhunter Nov 12 '16 at 22:27

0 Answers0