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 :)
#include <iostream>
#include <string>
usingnamespace 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:
virtualvoid clear() = 0;
virtualbool insert(const Elem&) = 0;
virtualbool append(const Elem&) = 0;
virtualbool remove(Elem&) = 0;
virtualvoid setStart() = 0;
virtualvoid setEnd() = 0;
virtualvoid prev() = 0;
virtualvoid next() = 0;
virtualint leftLength() const = 0;
virtualint rightLength() const = 0;
virtualbool setPos(int pos) = 0;
virtualbool getValue(Elem&) const = 0;
virtualvoid 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) returnfalse;
else {
it = listArray[fence + 1];
returntrue;
}
}
bool insert(const Elem& item)
{
if (listSize == maxSize) returnfalse;
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
returntrue;
}
bool append(const Elem& item)
{
if (listSize == maxSize) returnfalse;
listArray[listSize++] = item;
returntrue;
}
bool remove(Elem& it)
{
if (rightLength() == 0) returnfalse;
it = listArray[fence]; // Copy Elem
for (int i = fence; i<listSize - 1; i++)
// Shift them down
listArray[i] = listArray[i + 1];
// Decrement size
listSize--;
returntrue;
}
virtualvoid 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 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?