C++ 跳过对象的循环列表
我正在制作一个Student对象列表,并希望对它们进行迭代并输出它们的值。我不知道为什么会跳过for循环。如有任何帮助/指导,将不胜感激。以下是循环:C++ 跳过对象的循环列表,c++,list,class,C++,List,Class,我正在制作一个Student对象列表,并希望对它们进行迭代并输出它们的值。我不知道为什么会跳过for循环。如有任何帮助/指导,将不胜感激。以下是循环: void studentInfo(list<Student> stuList) { cout << "in studentinfo" << endl; for (list<Student>::iterator it = stuList.begin(); it != stuList.e
void studentInfo(list<Student> stuList) {
cout << "in studentinfo" << endl;
for (list<Student>::iterator it = stuList.begin(); it != stuList.end(); ++it) {
cout << "in student info loop" << endl;
cout << it->toString();
}
cout << "after loop" << endl;
}
void studentInfo(列表){
所以,当您使用调试器单步执行代码时,您会看到什么?列表是否为空?根据列表的填充方式进行编辑。我不确定如何使用调试器。不过,如果您有任何资源可以这样做,我正在使用visual studio。您的addToList
仅添加到(函数)传入的列表的本地副本
,而不是“原件”。同时搜索“visual studio调试”会让您直接访问官方文档:哦!谢谢,我在阅读您的评论后能够使其正常工作。我一定会仔细阅读调试文档。
string Student::toString() {
stringstream outString;
outString << "Name: " << name << "\nID: " << id << "\nAge: " << age << endl;
return outString.str();
}
void addToList(list<Student> stuList) {
string tempName;
int tempID;
int tempAge;
int numStudents;
cout << "How many students will you be entering?\n";
cin >> numStudents;
for (int i = 0; i < numStudents; i++) {
cout << "Enter student name: \n";
cin >> tempName;
cout << "Enter student id: \n";
cin >> tempID;
cout << "Enter student age: \n";
cin >> tempAge;
stuList.push_back(Student(tempName, tempID, tempAge));
}
}