简体   繁体   English

C ++:引发异常:按数组读取访问冲突

[英]C++: Exception thrown: Read access violation by array

I have an if statement that whenever I implement it, it throws a "read access violation" exception, even though all my other if-statements look pretty similar and work. 我有一个if语句,即使我执行的所有其他if语句看起来都非常相似且可以正常工作,只要执行它,它都会引发“读取访问冲突”异常。 Everything else works perfectly, so it perplexes me as to why this is happening. 其他所有东西都运行良好,所以让我困惑为什么会这样。

if (records[i].studentArray[j].id == records[i-1].studentArray[j].id) //these two
                records[i].retained++;

Full function 功能齐全

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;
}

Student and semester classes in the .h files .h文件中的学生和学期班

struct Semester
{
string semesterName;
int numStudents;
int fycount = 0;
int socount = 0;
int jrcount = 0;
int srcount = 0;
int retained = 0;
Student *studentArray;
};

class Student
{
public:
    string id;
    string year;
    string name;
    string lastName;
    friend istream & operator>> (istream &in, Student &s);
};

Thank you! 谢谢!

Do a trace of your application. 跟踪您的应用程序。 You will see that i is set to 0 initially and hence -1 th location will be invalid in an array. 您将看到i最初设置为0,因此数组中的第-1个位置将无效。

if (records[i].studentArray[j].id == records[i-1].studentArray[j].id)

That's the source of the error. 这就是错误的根源。 Better change it to: 最好将其更改为:

if (i > 0 && records[i].studentArray[j].id == records[i-1].studentArray[j].id)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM