C++ C++;越界误差
我试图将我的私有类中存在的某个结构推送到类类型记录的向量中。我在main函数中获取变量数据,但由于某些原因,在尝试将信息复制到结构中时,我不断得到越界错误。如果你能解释一下我将结构推入类向量的方法中的错误,那就太好了。。。我还包括了我计划的打印功能 下面是课程:C++ C++;越界误差,c++,arrays,debugging,vector,C++,Arrays,Debugging,Vector,我试图将我的私有类中存在的某个结构推送到类类型记录的向量中。我在main函数中获取变量数据,但由于某些原因,在尝试将信息复制到结构中时,我不断得到越界错误。如果你能解释一下我将结构推入类向量的方法中的错误,那就太好了。。。我还包括了我计划的打印功能 下面是课程: class students { public: // All necessary member functions here students(int RUID, string first_name, string l
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;
};
班级学生
{
公众:
//这里有所有必要的成员函数
学生(整数、字符串名字、字符串姓氏、向量测验分数、数组考试分数)
{
记录;
records.RUID=RUID;
records.first\u name=first\u name;
records.last_name=last_name;
对于(int i=0;i cout在students::students()
中
for(int i=0;0<3;++i)
//^^^^应该是'i'`
显然是错误的。这清楚地解决了我在数组中的问题;但是我仍然得到了向量超出范围的问题。@Djroverrecords.quick\u grades[I]=quick\u grades[I];
应该是records.push\u(quick\u grades[I]);
因为记录。quick\u grades不包含元素(size=0),因为它是刚刚创建的,现在仍然是空的,并且您正试图从中访问(不存在的)元素。
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;
for (int i = 0; 0 < 3; ++i)
// ^^^ should be `i`