Progam blows up during clean up

I have been working on this problem for four days and though I've made progress I still have not solved the problem. The quick and short of it is that I have a linked list that I am to appending nodes to. The linked list is made up of a derived class called CD. It is derived from a class called Media. The CD class has a member that is a linked list. I can get the nodes to append just fine, but when the program closes it throws an error telling me that it is trying to free memory that has already been freed. I have been studying copy constructors and copy assignment operators and have written both for Media class and CD class. I have a copy constructor for LinkList that I believe performs a deep copy of the linked list. I am struggling to write a copy assignment operator for it but I'm not sure I need one. If I do I will need help writing it because of all the examples I have seen few were correct and some I couldn't follow. I will post what I have for the copy constructors and copy assignment operators. I would really appreciate anyone looking them over and letting me know if they are incorrect. I will post whatever code anyone needs here or if someone is willing to run the program I have links to all the program files here:

http://pastebin.com/8ycsasf6 //main program file
http://pastebin.com/UFuXL2P9 //CD header file
http://pastebin.com/wbMMJz0H //Media header file
http://pastebin.com/VQ6JVNQE //LinkList header file

Here are links to other forums I have used(the classes are the same but some other code may be old/different from the files above):

http://www.cplusplus.com/forum/beginner/131872/
http://www.cplusplus.com/forum/beginner/131373/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  //***Copy Constructor***
template <class T> 
LinkList<T>::LinkList( const LinkList &listObj )
{

	head = NULL;

	ListNode *nodePtr;

	nodePtr = listObj.head;

	while(nodePtr != NULL)
	{
		 appendNode(nodePtr->value1);
		
		 nodePtr = nodePtr->next;
	}

	cout<<"LinkList Copy Constructor"<<endl;
}

//****Copy assignment Operator***
template <class T>
LinkList<T>& LinkList<T> :: operator=(const LinkList<T> &other)
{
	//????
}

//***Copy Constructor***
Media::Media(const Media &obj)
	:
	title(obj.title),
	length(obj.length)
{cout<<"Media Copy Constructor"<<endl;}



//***Copy Assignment Operator***
Media Media::operator = (const Media &obj)
{
	title = obj.title;
	length = obj.length;
	cout<<"Media copy assignment operator"<<endl;
	return *this;
}

//***Copy Constructor***
CD::CD(const CD &obj) 
	
	:Media(obj),
	
	Cd(obj.Cd)	

{cout<<"CD Copy Constructor"<<endl;}

//***Copy Assignment Operator***
CD CD::operator = (const CD &obj)
{
	Media::operator=(obj);

	Cd = obj.Cd;
			
	cout<<" CD Assignment operator called "<<endl;
	return *this;
}


Please any help would be great(especially if someone would run this).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>

template <class T> class LinkList
{
private:

    struct node
    {
        explicit node( const T& v, node* n = 0 ) : value(v), next(n) {}
        T value ;
        node *next ;
    };

    node* head;   // List head pointer
    node* tail; // keepinm track of the tail makes a lot of things efficient

public:

    bool empty() { return head == 0 ; }

    LinkList() : head(0), tail(0) {}

    LinkList( const LinkList<T>& that ) : head(0), tail(0)
    { for( node* n = that.head ; n != 0 ; n = n->next ) append(n->value) ; }

    ~LinkList() { clear() ; }

    LinkList& operator=( const LinkList & that )
    {
        if( head != that.head ) // if not trivial self-assignment
        {
            clear() ; // what the destructor does

            // what the copy constructor does
            for( node* n = that.head ; n != 0 ; n = n->next ) append(n->value) ;
        }
        return *this ;
    }

    void clear() // empty the list
    {
        while( !empty() )
        {
            node* n = head ;
            head = head->next ;
            delete n ;
        }
    }

    void append( const T& v )
    {
        if( !empty() ) { tail->next = new node(v) ; tail = tail->next ; }
        else head = tail = new node(v) ;
    }

    void insert( const T& v )
    {
        if( empty() || tail->value < v ) append(v) ;

        else if( head->value > v ) head = new node(v,head) ;

        else
        {
            node* n = head ;

            // bump n till the next node has a larger value
            for( ; n->next->value <= v ; n = n->next ) ;

            n->next = new node( v, n->next ) ; // insert after n
        }
    }

    // etc.

    void print() const
    {
        if( head == 0 ) std::cout << "[empty]\n" ;
        else
        {
            std::cout << "[ " ;
            for( node* n = head ; n != 0 ; n = n->next ) std::cout << n->value << ' ' ;
            std::cout << "]\n" ;
        }
    }
};

int main()
{
    LinkList<int> lst ;
    lst.print() ; // [empty]

    for( int i = 0 ; i < 5 ; ++i ) lst.append(i) ;
    lst.print() ; // [ 0 1 2 3 4 ]

    lst.insert(-7) ;
    lst.insert(7) ;
    lst.insert(3) ;
    lst.print() ; // [ -7 0 1 2 3 3 4 7 ]

    LinkList<int> lst2 = lst ;
    lst2.print() ; // [ -7 0 1 2 3 3 4 7 ]

    lst.clear() ;
    lst.append(87) ;
    lst.insert(23) ;
    lst.print() ; // [ 23 87 ]

    lst = lst2 ;
    lst.print() ; // [ -7 0 1 2 3 3 4 7 ]
}

http://coliru.stacked-crooked.com/a/02af4cb605ff4fa8
EUREKA!!! IT WORKS!! Thank you cire, JLBorges, AbstractAnon and everyone else who helped me get this linked list to work. I learned a lot about linked list, copy constructors and assignement operators! Though frustrating it has also been fun!! Thanks again!!
Topic archived. No new replies allowed.