我有一个if语句,每当我实现它时,它都会引发"读取访问冲突"异常,即使我的所有其他if语句看起来非常相似并且有效。其他一切都很完美,所以让我感到困惑的是为什么会发生这种情况。
if let userInfo = Auth.auth().currentUser {
userInfo.reload(completion: { (error) in
guard error == nil else {
debugPrint(error.debugDescription)
return
}
})
}
全功能
if (records[i].studentArray[j].id == records[i-1].studentArray[j].id) //these two
records[i].retained++;
.h文件中的学生和学期课程
void readFile()
{
//Function reads the file and displays all results
int i = 0;
int j = 0;
int count;
Semester records[25]; //only holds up to 25 semesters
Student studentArray;
string semesterName;
ifstream roster;
roster.open ("records.txt");
if (roster.is_open())
{
roster >> semesterName;
while(!roster.eof())
{
roster >> count;
records[i].semesterName = semesterName;
cout << semesterName;
records[i].numStudents = count;
records[i].studentArray = new Student[count];
for (j = 0; j < count; j++)
{
roster >> records[i].studentArray[j];
if (records[i].studentArray[j].year == "FY")
records[i].fycount++;
else if (records[i].studentArray[j].year == "SO")
records[i].socount++;
else if (records[i].studentArray[j].year == "JR")
records[i].jrcount++;
else if (records[i].studentArray[j].year == "SR")
records[i].srcount++;
if (records[i].studentArray[j].id == records[i-1].studentArray[j].id)
records[i].retained++;
}
cout << endl;
cout << "FY: " << records[i].fycount << endl;
cout << "SO: " << records[i].socount << endl;
cout << "JR: " << records[i].jrcount << endl;
cout << "SR: " << records[i].srcount << endl;
cout << "FY gain/loss: " << records[i].fycount - records[i - 1].fycount << endl;
cout << "SO gain/loss: " << records[i].socount - records[i - 1].socount << endl;
cout << "JR gain/loss: " << records[i].jrcount - records[i - 1].jrcount << endl;
cout << "SR gain/loss: " << records[i].srcount - records[i - 1].srcount << endl;
cout << "Percentage gained/lost: " << static_cast<double>(records[i].numStudents - records[i - 1].numStudents) / records[i - 1].numStudents * MULTIPLIER << "%" << endl;
cout << "Number of students retained: " << records[i].retained << endl;
cout << endl;
delete[] records[i].studentArray;
roster >> semesterName;
i++;
}
roster.close();
}
else
cout << "Error: File not found" << endl;
}
谢谢!
答案 0 :(得分:0)
跟踪您的申请。您将看到i
最初设置为0,因此-1
位置在数组中无效。
if (records[i].studentArray[j].id == records[i-1].studentArray[j].id)
这是错误的来源。最好将其改为:
if (i > 0 && records[i].studentArray[j].id == records[i-1].studentArray[j].id)
希望这有帮助。