获得访问违规的阅读位置,但不确定为什么
Getting Access violation reading location but not sure why
我正在处理C 类分配。我正在获得违规访问读取地点0xcdcdcdcdcd。我不知道什么。
这是定义指针数组的花名册构造函数。根据说明,它必须成为一系列指针。
roster::roster(int capacity)
{
this->capacity = capacity;
this->lastIndex = -1;
this->students = new student*[capacity];
}
这是我的添加方法。在调试中,所有值都是正确的。即使我启动学生似乎在那里,但是在超过这一点之后,它具有不同的指针地址。
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->学生([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++;
}
}
我无法阅读。
在此处显示正确。
每个请求。这是Student.CPP和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();
}
尝试使用for for loop明确初始化构造函数中的所有指针。
相关文章:
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 不确定如何装饰我的C++库代码以在 C 中使用
- C++功能泄漏内存,我是C++新手,不确定如何解决
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 为什么以下代码是不确定的?
- 向量的内存位置不连续
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 误用指针会产生分段错误。我仍然不确定如何纠正它
- 我不确定为什么代码没有运行,它在编译时抛出错误,说它无法找到 PrintArray()
- 不确定出了什么问题(字符串输入)
- C++睡眠时间不确定
- 在C++中做一个场景问题,我不确定如何处理布尔和if循环
- 代码输出随机符号,我不确定出了什么问题
- 可变值变化,不确定原因
- 有没有办法让C++从 cin 中接收不确定数量的字符串?
- 尝试一次将单个字符读取到大小不确定的数组中
- 我不确定如何引用此神经网络训练方法中的权重
- 获得访问违规的阅读位置,但不确定为什么
- C++ 不确定如何让我的程序输出空间的位置