我曾在几个不同的论坛上尝试过,但似乎无法得到一个直接的答案,如何使此函数返回结构?如果我尝试“返回newStudent;”我收到错误消息“不存在从studentType到studentType的适当的用户定义转换。”
// Input function
studentType newStudent()
{
struct studentType
{
string studentID;
string firstName;
string lastName;
string subjectName;
string courseGrade;
int arrayMarks[4];
double avgMarks;
} newStudent;
cout << "\nPlease enter student information:\n";
cout << "\nFirst Name: ";
cin >> newStudent.firstName;
cout << "\nLast Name: ";
cin >> newStudent.lastName;
cout << "\nStudent ID: ";
cin >> newStudent.studentID;
cout << "\nSubject Name: ";
cin >> newStudent.subjectName;
for (int i = 0; i < NO_OF_TEST; i++)
{ cout << "\nTest " << i+1 << " mark: ";
cin >> newStudent.arrayMarks[i];
}
newStudent.avgMarks = calculate_avg(newStudent.arrayMarks,NO_OF_TEST );
newStudent.courseGrade = calculate_grade (newStudent.avgMarks);
}
最佳答案
您遇到范围问题。在函数之前而不是在函数内部定义结构。