编译错误:无法将参数 1 从 'student[20]' 转换为 'student'

Compilation error: cannot convert argument 1 from 'student[20]' to 'student'

本文关键字:student 转换 错误 参数 编译      更新时间:2023-10-16

我编写了一部分代码,该代码应该接收一个数据文件并将数据放入一个结构数组中。我已经得到了进入他们各自地方的数据,但我很难找到确定文件中学生人数的方法。这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct student{
string fname, lname, id, classname[20];
char grade[20];
int units[20], unitstaken, unitscompleted, numberofclasses;
float gpa, points[20], avg;
};
void doesitrun(ifstream& fin, string& filename);
void readit(student& temp, ifstream& fin);
int Read_List(student child, int size);
int main(){
int i = 0, size = 0;
ifstream fin;
string filename;
student u[20];
doesitrun(fin, filename);
size = Read_List(u[20], 20);
for (i; i < 3; i++){
readit(u[i], fin);
}

cout << size << endl;
cout << u[2].classname[11];
cout << u[2].grade[11] << endl;
}
void doesitrun(ifstream& fin, string& filename){
bool trueorfalse;
cout << "Enter file name along with location: ";
getline(cin, filename);
cin.ignore(20, 'n');
fin.open(filename.c_str());
if (fin.fail())
trueorfalse = false;
else
trueorfalse = true;
while (trueorfalse == false){
cout << "File does not run. Please try again. " << endl;
fin.close();
cout << "Enter file name along with location: ";      
getline(cin, filename);
fin.open(filename.c_str());
if (fin.fail())
trueorfalse = false;
else
trueorfalse = true;
}
}
void readit(student& temp, ifstream& fin){
int i = 0;
getline(fin, temp.lname, ',');
cin.ignore();
getline(fin, temp.fname);
fin >> temp.id;
fin >> temp.numberofclasses;
fin.ignore(10, 'n');
for (i = 0; i < temp.numberofclasses; i++){
getline(fin, temp.classname[i]);
fin >> temp.grade[i];
fin >> temp.units[i];
fin.ignore(10, 'n');
}
}

int Read_List(student child, int size)
{
ifstream fin;
int      i = 0;
readit(child, fin);
while (!fin.eof())
{
i++;
if (i >= size)
{
cout << "Array is full.n";
break;
}
readit(child, fin);
}
fin.close();
return i;
}

这是一个我将使用的数据文件示例:

Smith Jr., Joe
111-22-3333 3
Physics I
A 5
English 1A
B 4
English 1B
F 4
Jones, Bill 
111-11-1111 4
Physics I
A 5
Chemistry 1A
B 5
Computer Science 1
A 4
Chemistry 1 Lab
B 1
Brown, Nancy
222-11-1111 13
Physics I
A 5
English 1A
B 4
English 1B
F 4
Physics II
A 5
Chemistry 1A
B 5
Computer Science 1
A 4
Chemistry 1A Lab
B 1
Physics I Lab
A 1
Physics II Lab
A 1
Physics III
A 5
Physics III Lab
A 1
Chemistry 1B
A 5
Chemistry 1B Lab
A 1

我想编辑我的代码,这样我就可以使用Read_List函数来查找文件中的学生人数。在这个例子中,大小是3。

我不确定示例中代码size = Read_List(u[20], 20);的用途,但不确定部分u[20],其中"20"是"u"变量数组位置的索引。因此,索引不能超过数组的大小,这意味着u[20]中的"20"必须小于20(例如19)。

向函数readit(student& temp, ifstream& fin)传递u[i]的参数必须是指针(或数组)的地址,但调用readit()似乎传递了指针的值。