C ++ Out of Bounds错误

时间:2016-03-13 16:49:08

标签: c++ arrays debugging vector

我试图将我的私有类中存在的某个结构推送到类类型记录的向量中。我在main函数中获取了变量数据,但由于某种原因,我在尝试将信息复制到struct时仍然遇到了一个越​​界错误。如果你可以解释我将方法推入类向量的方法中的错误,那将是很好的...我也包括了我计划的打印函数。

这是班级:

class students
{
public:
    // All necessary member functions here
    students(int RUID, string first_name, string last_name, vector<double> quiz_grades, array<double, 3> exam_grades)
    {
        record records;
        records.RUID = RUID;
        records.first_name = first_name;
        records.last_name = last_name;


        for (int i = 0; i < quiz_grades.size(); ++i)
        {
            records.quiz_grades[i] = quiz_grades[i];
        }


        for (int i = 0; 0 < 3; ++i)
        {
            records.exam_grades[i] = exam_grades[i];
        }

        student_records.push_back(records);
    }


    void printRecords()
    {
        //vector<record>::iterator it = student_records.begin(); it != student_records.end(); ++it
        for (unsigned int i = 0; i < student_records.size(); ++i)
        {
            cout << "Ruid: " << student_records[i].RUID << endl;
            cout << "First Name: " << student_records[i].first_name << endl;
            cout << "Last Name: " << student_records[i].last_name << endl;
            for (unsigned int j = 0; j < student_records[i].quiz_grades.size(); ++j)
            {
                cout << "Quiz grade " << j + 1 << " is: " << student_records[i].quiz_grades[j] << endl;
            }
            for (int k = 0; k < 3; ++k)
            {
                cout << "Test grade " << k + 1 << " is: " << student_records[i].exam_grades[k] << endl;
            }
        }
    }
    // using the friend function in class 
    friend void change_name(students stdn); // passing all necessary inputs 

private:
    struct record
    {
        int RUID;
        string first_name;
        string last_name;
        vector<double> quiz_grades;
        array<double, 3> exam_grades = { 0,0,0 };
    };
    vector<record> student_records;
};

这是我的主要职能:

int main()
{
    string input;
    bool quit = false;
    int RUID;
    string first;
    string last;
    double grade = 100;

    vector<double> quizG;
    array <double, 3> examG = { 0, 0, 0 };

    cout << " --'new' to add, 'quit' to end--" << endl;

    while (quit != true)
    {

        cout << "Please enter would you would like to do: ";
        cin >> input;
        cout << endl;


        if (input == "quit")
        {
            quit = true;
            break;
        }

        if (input == "new")
        {
            cout << "Please enter the RUID: ";
            cin >> RUID;
            cout << endl << "Please enter the first name: ";
            cin >> first;
            cout << endl << "Please enter the last name: ";
            cin >> last;
            cout << "Enter quiz grades. Enter number less than 0 to stop." << endl;
            while (grade >= 0)
            {
                cout << "Enter quiz grade: ";
                cin >> grade;
                if (grade >= 0)
                {
                    quizG.push_back(grade);
                }
                else if (grade < 0)
                {
                    break;
                }
            }
            for (int i = 0; i < 3; ++i)
            {
                cout << "Please enter " << i + 1 << " test grade: ";
                cin >> grade;
                examG[i] = grade;
            }
        }
        students stdn(RUID, first, last, quizG, examG);
        //stdn.printRecords();
    }



    //  change_name(stdn);

    return 0;

1 个答案:

答案 0 :(得分:3)

students::students()

for (int i = 0; 0 < 3; ++i)
           //  ^^^ should be `i`

显然是错误的。