_block_type_is_valid(phead- nblockuse) line 52

This is my code. Apparently the error is line 52 and i have no idea how to solve it :( I read a bunch of posts that's it's due to deleting a pointer twice or something but i have no idea how does that relate to my code. any help would be appreciated :)

Classes:

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <string>
using namespace std;

int DefaultListSize = 20;

class Student
{
private:
    int id;
    string major;
public:
    Student(int a = 0, string b = " ")
    {
        id = a;
        major = b;
    }
    int GetId()
    {
        return id;
    }
    string GetMajor()
    {
        return major;
    }
    void SetStudent()
    {
        int a;
        string b;
        cout << "\nPlease enter the id of the student:\n";
        cin >> a;
        cout << "\nPlease enter the major of the student:\n";
        cin >> b;
        id = a;
        major = b;
    }
    void SetStudent(int a, string b)
    {
        id = a;
        major = b;
    }
    void print()
    {
        cout << "\nThis student has the following id: " << id << " and the following major: " << major << ".\n";
    }
};

class Major
{
private:
    string code;
    string description;
public:
    Major(string a = " ", string b = " ")
    {
        code = a;
        description = b;
    }
    string GetCode()
    {
        return code;
    }
    string GetDescription()
    {
        return description;
    }
    void SetMajor()
    {
        string a;
        string b;
        cout << "\nPlease enter the code of the major:\n";
        cin >> a;
        cout << "\nPlease enter the description of the major:\n";
        cin >> b;
        code = a;
        description = b;
    }
    void SetMajor(string a, string b)
    {
        code = a;
        description = b;
    }
    void print()
    {
        cout << "\nThis major has the following code: " << code << " and the following description: " << description << ".\n";
    }
};

template <class Elem>
class List
{
public:
    virtual void clear() = 0;
    virtual bool insert(const Elem&) = 0;
    virtual bool append(const Elem&) = 0;
    virtual bool remove(Elem&) = 0;
    virtual void setStart() = 0;
    virtual void setEnd() = 0;
    virtual void prev() = 0;
    virtual void next() = 0;
    virtual int leftLength() const = 0;
    virtual int rightLength() const = 0;
    virtual bool setPos(int pos) = 0;
    virtual bool getValue(Elem&) const = 0;
    virtual void print() const = 0;
};

template <class Elem> // Array-based list
class AList : public List<Elem>
{
private:
    int maxSize;     // Maximum size of list
    int listSize;    // Actual elem count
    int fence;       // Position of fence
    Elem* listArray; // Array holding list
public:
    AList(int size = DefaultListSize)
    {
        maxSize = size;
        listSize = fence = 0;
        listArray = new Elem[maxSize];
    }
    AList(const AList &obj) //copy constructor
    {
        maxSize = obj.maxSize;
        listSize = fence = 0;
        listArray = new Elem[maxSize];
        *listArray = *obj.listArray;
    }
    ~AList() { delete[] listArray; }
    void clear()
    {
        delete[] listArray;
        listSize = fence = 0;
        listArray = new Elem[maxSize];
    }
    void setStart() { fence = 0; }
    void setEnd() { fence = listSize - 1; }
    void prev()   { if (fence != 0) fence--; }
    void next()   {
        if (fence < listSize)
            fence++;
    }
    int leftLength() const  { return fence + 1; }
    int rightLength() const
    {
        return listSize - fence - 1;
    }
    bool setPos(int pos)
    {
        if ((pos >= 0) && (pos < listSize))
            fence = pos;
        return (pos >= 0) && (pos < listSize);
    }

    bool getValue(Elem& it) const
    {
        if (rightLength() == 0) return false;
        else {
            it = listArray[fence + 1];
            return true;
        }
    }
    bool insert(const Elem& item)
    {
        if (listSize == maxSize) return false;

        for (int i = listSize; i>fence; i--)
            // Shift Elems up to make room
            listArray[i] = listArray[i - 1];

        listArray[fence + 1] = item;
        listSize++; // Increment list size
        return true;
    }
    bool append(const Elem& item)
    {
        if (listSize == maxSize) return false;
        listArray[listSize++] = item;

        return true;
    }
    bool remove(Elem& it)
    {
        if (rightLength() == 0) return false;

        it = listArray[fence]; // Copy Elem

        for (int i = fence; i<listSize - 1; i++)
            // Shift them down
            listArray[i] = listArray[i + 1];

        // Decrement size
        listSize--;
        return true;
    }
    virtual void print() const
    {
        Elem a;
        if (getValue(a)) a.print();
    }
};


Main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    //AList<Major> ListM;
    AList<AList<Student>> ListA;

    cout << "Hello dear user,\nDuring this program you will be able to do the following at any time:\n";
    cout << "1- Add a new student\n2- Print students in any given major\n3- Print students in all majors\n4- Remove any student from any major\n";

    cout << "\nFirst how about adding your first student?\n";

    AList<Student> A;
    Student a;
    a.SetStudent();
    if (A.insert(a)) cout << "yes\n";
    if (ListA.insert(A)) cout << "yes\n";
    system("pause");
    return 0;
}
I have used debugging and my program gets the error when deleting the listarray of AList<Student> A.
How can i fix this?
I figured what happened and i guess ListA is being deleted by the destructor and then the destructor is trying to delete A which has already been deleted the first time it got called.
How can i prevent this from happening?
Topic archived. No new replies allowed.