I'm having a run-time error with memory access. I tried to shorten my code below to highlight what's important, hopefully I didn't take out what's useful to analyse the problem.
Debug
For some reason I'm getting segmentation fault. I searched up the reasoning and it has to do with accessing memory that I don't have access to. However, the statement std::cout << d1.getPatientNameAtPosition(2) << "\n"; is simply accessing the pointerArray in the private member of the Doctor.
Could anyone see what's wrong with my code that causes this problem?
You were right that wasn't it. It was because of my destructor and I didn't include it in my code. My mistake! XD
It basically looked like this
1 2 3
~Doctor::Doctor{
delete[] patientArray;
}
This would end up for some reason deleting my pointerArray beforemy function getPatientNameAtPosition ran.
EDIT: The error did not reside in the Destructor but actually in my addPatient function. My addPatient function was if (maxArraySize < 20 ). But maxArraySize is 20 therefore the if condition is never fulfilled and the Array is never written.