如何解决C ++一张一张地传递一个结构数组-如此简单,但我很困惑
|解决方法
您已声明为“ 0”,这似乎是错字。随处更改为
StudentRecord
。,您的原型还有一个逗号。这行:
void PrintRecords(studenRec&,myRec);
应该:
void PrintRecords(studenRec& myRec);
,您的职能
void PrintRecords(studentRec& myRec)
只需引用结构studentRec,而不是数组。所以当你使用代码
for (int i = 0; i < 3; i++) {
PrintRecords(myRec[i]);
}
您说的是“遍历数组中的所有三个项,并将每个实例一个接一个地传递给函数PrintRecords”,这意味着只要调用PrintRecords,它就只会访问其中一个实例。
但是,在函数printRecords中,您实际上访问myRec就像是一个数组,而实际上它只是对恰好位于数组中的对象的单个引用。正确的功能应如下所示:
void PrintRecords(studentRec& myRec)
{
cout << \"Students name: \" << myRec.name << endl;
cout << \"Students address: \" << myRec.address << endl;
cout << \"Students city: \" << myRec.city << endl;
cout << \"Students state: \" << myRec.state << endl;
cout << \"Students zip: \" << myRec.zip << endl;
cout << \"Students gender: \" << myRec.gender << endl;
cout << \"Students id: \" << myRec.myid << endl;
cout << \"Students gpa: \" << myRec.gpa << endl;
cout << endl;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。