我正在尝试编写一个程序来使用结构数组执行各种操作,但我遇到了函数问题。我不断收到错误 E2235“必须调用成员函数或在函数读取中获取其地址(学生 *,int)”,我不确定是什么导致了这个错误。我的代码如下:
#include <iostream.h>
#include <fstream>
struct student{
string first;
string last;
string id;
double GPA;
int age;
};
void menu();
void read(student* data, int x);
int main(){
bool quit=false;
char choice;
student data[20];
cout<<"Welcome!"<<endl;
//the program is menu driven, so a switch statement allows for many choices
while(!quit){
menu();
cin>>choice;
switch(choice){
case '1': read(data, 20); break;
case '2': break;
case '3': break;
case '4': break;
case '5': break;
case '6': break;
case '7': break;
case '8': cout<<endl<<"Goodbye!\n"; quit=true; break;
default: cout<<"I think you're doing it wrong. Please try again.";
}
}
}
//menu output so the user knows what they're doing
void menu(){
cout<<endl;
cout<<"What would you like to do?"<<endl;
cout<<"(1) - Read data into the database"<<endl;
cout<<"(2) - Add a student"<<endl;
cout<<"(3) - Remove a student"<<endl;
cout<<"(4) - List all students"<<endl;
cout<<"(5) - Compute the average GPA and SD of the database"<<endl;
cout<<"(6) - View Student Information"<<endl;
cout<<"(7) - Save the database"<<endl;
cout<<"(8) - Exit"<<endl;
}
//the problem function
void read(student* data, int x){
ifstream infile;
infile.open("StudentStruct.txt");
int k=0;
string first, last, id;
double GPA;
int age;
while(!infile.eof&&k<x){
infile>>first;
data[k].first;
infile>>last;
data[k].last;
infile>>id;
data[k].id;
infile>>GPA;
data[k].GPA;
infile>>id;
data[k].age;
k++;
}
cout<<"The database now contains "<<k<<" data entries from 'StudentStruct.txt'"<<endl;
}
什么可能导致此错误,我应该如何解决它?