I am having issues with the destructor of my class in this code. It is saying that is was never allocated, however it should have been, and I never delete it myself. Here is snippets of the code:
#ifdef UNIT_TESTING_CONSTRUCTORS
//Test Constructors
cout << "Test constructors \nConctructor 1:\n";
Doctor testDoc1;
testDoc1.displayPatientArray();
cout << "\nConstructor 2:\n";
Doctor testDoc2(2);
testDoc2.displayPatientArray();
cout << "\nConstructor 3:\n";
//Implement more test cases below:
Doctor testDoc3("Wesley Cates");
testDoc3.displayPatientArray();
cout << "\nConstructor 4:\n";
Doctor testDoc4("Baylor Bishop", 3);
testDoc4.displayPatientArray();
#endif
Doctor::Doctor() : doctorName("need a name."), patientArraySize(100), numOfPatient(0) {
//Create a dynamic array for patients below:
//stringPtr_t* pArray;
stringPtr_t* patientArray;
patientArray = new stringPtr_t[patientArraySize];
And the class:
typedef unsigned short ushort_t;
typedef string* stringPtr_t;
class Doctor {
private:
string doctorName;
stringPtr_t patientArray;
ushort_t patientArraySize;
ushort_t numOfPatient;
public:
Doctor();
Doctor(ushort_t patientArrayCapacity);
Doctor(string docName);
Doctor(string docName, ushort_t patientArrayCapacity);
bool addPatient(string patientName);
void displayPatientArray();
void resizePatientArray(ushort_t newArraySize);
string getDoctorName() const {return doctorName;}
ushort_t getNumOfPatient() const {return numOfPatient;}
ushort_t getArraySize() const {return patientArraySize;}
void setDoctorName(string docName) {doctorName.assign(docName);};
void emptyPatientArray() {numOfPatient = 0;}
Doctor& operator =(const Doctor& docSource);
~Doctor() {delete [] patientArray;}
};
The array you are initialising in your constructor Doctor::Doctor()
is a local variable called 'patientArray', not the class variable that you are then deleting in the destructor.
To fix the problem, change the constructor to this:
Doctor::Doctor() : doctorName("need a name."), patientArraySize(100), numOfPatient(0) {
// Create a dynamic array for patients below:
// stringPtr_t* pArray;
// Delete local variable declaration that was here: stringPtr_t* patientArray;
//
patientArray = new string[patientArraySize];