Reputation: 31
I am working on a c++ class assignment. I am getting the Access violation reading location 0xCDCDCDCDCD. I can't figure out what.
Here is the roster constructor where the array of pointers is defined. It has to be and array of pointers per the instructions.
roster::roster(int capacity)
{
this->capacity = capacity;
this->lastIndex = -1;
this->students = new student*[capacity];
}
Here is my add method. In debug all the values are correct. Even when I new up the students it seems like it is there but it has a different pointer address after it gets past that point.
void roster::add(string studentId, string firstName, string lastName, string emailAddress, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, Degree degreeType)
{
int openArrayIndex = 0;
for (int i = 0; i < capacity; i++) {
if (students[i] == NULL) {
break;
}
else {
openArrayIndex++;
}
}
int daysInCourses[3]{ daysInCourse1, daysInCourse2, daysInCourse3 };
switch (degreeType)
{
case SECURITY: {
students[openArrayIndex] = new securityStudent(studentId, firstName, lastName, emailAddress, age, daysInCourses, degreeType);
break;
}
case SOFTWARE: {
students[openArrayIndex] = new softwareStudent(studentId, firstName, lastName, emailAddress, age, daysInCourses, degreeType);
break;
}
case NETWORK: {
students[openArrayIndex] = new networkStudent(studentId, firstName, lastName, emailAddress, age, daysInCourses, degreeType);
break;
}
}
this->lastIndex = openArrayIndex;
}
This is where the exception actually occurs on the (this->students)[i]->print();
void roster::printAll()
{
int currentArrayIndex = 0;
for (int i = 0; i <= lastIndex; i++) {
if ((this->students)[i] != NULL) {
(this->students)[i]->print();
}
currentArrayIndex++;
}
}
Per request. Here is student.cpp and secuirityStudent.cpp.
student::student()
{
this->studentId = "";
this->firstName = "";
this->lastName = "";
this->emailAddress = "";
this->age = 0;
for (int i = 0; i < daysInCoursesArrSize; i++)
this->daysInCourses[i] = 0;
}
student::student(string studentId, string firstName, string lastName, string emailAddress, int age, int daysInCourses[])
{
this->studentId = studentId;
this->firstName = firstName;
this->lastName = lastName;
this->emailAddress = emailAddress;
this->age = age;
for (int i = 0; i < daysInCoursesArrSize; i++)
this->daysInCourses[i] = daysInCourses[i];
}
void student::SetStudentId(string studentId) { this->studentId = studentId; }
string student::GetStudentId() { return studentId; }
void student::SetFirstName(string firstName) { this->firstName = firstName; }
string student::GetFirstName() { return firstName; }
void student::SetLastName(string lastName) { this->lastName = lastName; }
string student::GetLastName() { return lastName; }
void student::SetEmailAddress(string emailAddress) { this->emailAddress = emailAddress; }
string student::GetEmailAddress() { return emailAddress; }
void student::SetAge(int age) { this->age = age; }
int student::GetAge() { return age; }
void student::SetDaysInCourses(int daysInCourses[]) {
for (int i = 0; i < daysInCoursesArrSize; i++)
this->daysInCourses[i] = daysInCourses[i];
}
int * student::GetDaysInCourses() { return daysInCourses; }
void student::print() {
cout << left << setw(10) << studentId;
cout << left << setw(20) << firstName;
cout << left << setw(20) << lastName;
cout << left << setw(30) << emailAddress;
cout << left << setw(10) << age;
cout << left << setw(10) << daysInCourses[0];
cout << left << setw(10) << daysInCourses[1];
cout << left << setw(10) << daysInCourses[2];
}
student::~student()
{
}
securityStudent::securityStudent() :student()
{
degreeType = SECURITY;
}
securityStudent::securityStudent(string studentId, string firstName, string lastName, string emailAddress, int age, int daysInCourses[], Degree degreeType)
: student(studentId, firstName, lastName, emailAddress, age, daysInCourses)
{
degreeType = SECURITY;
}
Degree securityStudent::GetDegreeType() { return SECURITY; }
void securityStudent::print() {
this->student::print();
cout << degreeTypeStrings[degreeType] << "\n";
}
securityStudent::~securityStudent()
{
student::~student();
}
Upvotes: 1
Views: 100
Reputation:
Try explicitely initializing all the pointers to null in the constructor using a for loop.
Upvotes: 1