我是 C++ 新手,目前正在尝试通过添加一个名为“TopStudents”的方法来扩展课程类,该方法旨在搜索学生对象数组以查找并打印 GPA 高于给定值的所有学生(可以找到在主程序中)。我已经标记了我遇到错误的行。
当我编译这个程序时,我收到以下错误消息:
---error: expected primary-expression before ‘int’
students[index].Get(int &uaid, string & name, float &gpa);
^~~
---error: expected primary-expression before ‘&’
students[index].Get(int &uaid, string & name, float &gpa);
^
---error: ‘name’ was not declared in this scope
students[index].Get(int &uaid, string & name, float &gpa);
^~~~
---error: expected primary-expression before ‘float’
students[index].Get(int &uaid, string & name, float &gpa);
^~~~~
---error: ‘gpa’ was not declared in this scope
if (gpa >= minGpa) {
^~~
这是我正在使用的输入:
1234 Susan 3.9
2345 John 3.2
3456 Laura 3.8
4567 Brian 3.5
5678 David 3.1
最后,这是我的代码:
////////////////////////////
///////////LAB C////////////INCOMPLETE
////////////////////////////
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
Student();
Student(const Student & student);
~Student();
void Set(const int uaid, const string name, const float gpa);
void Get(int & uaid, string & name, float & gpa) const;
void Print() const;
void Read();
private:
int Uaid;
string Name;
float Gpa;
};
Student::Student()
{
Uaid = 0;
Name = "none";
Gpa = 0;
}
Student::Student(const Student & student)
{
Uaid = student.Uaid;
Name = student.Name;
Gpa = student.Gpa;
}
Student::~Student()
{
}
void Student::Set(const int uaid, const string name, const float gpa)
{
Uaid = uaid;
Name = name;
Gpa = gpa;
if (Gpa < 0.0) Gpa = 0.0;
else if (Gpa > 4.0) Gpa = 4.0;
}
void Student::Get(int &uaid, string & name, float &gpa) const
{
uaid = Uaid;
name = Name;
gpa = Gpa;
}
void Student::Print() const
{
cout << Uaid << " " << Name << " " << Gpa << endl;
}
void Student::Read()
{
cin >> Uaid >> Name >> Gpa;
if (Gpa < 0.0) Gpa = 0.0;
else if (Gpa > 4.0) Gpa = 4.0;
}
const int MAX_STUDENTS = 100;
class Course
{
public:
Course(const int count=0);
Course(const Course & course);
~Course();
void Print() const;
void Read();
void TopStudents(float minGpa);
private:
Student students[MAX_STUDENTS];
int num_students;
};
Course::Course(const int count)
{
cout << "Constructor" << endl;
num_students = count;
}
Course::Course(const Course & course)
{
cout << "Copy constructor" << endl;
for (int index = 0; index < num_students; index++)
{
}
}
Course::~Course()
{
cout << "Destructor" << endl;
}
void Course::Print() const
{
cout << "Print" << endl;
for (int index = 0; index < num_students; index++)
{
students[index].Print();
}
}
void Course::Read()
{
cout << "Read" << endl;
for (int index = 0; index < num_students; index++)
{
students[index].Read();
}
}
void Course::TopStudents(float minGpa)
{
cout << "TopStudents" << endl;
for (int index = 0; index < num_students; index++)
{
students[index].Get(int &uaid, string & name, float &gpa); // ERROR
if (gpa >= minGpa) { // ERROR
students[index].Print();
}
}
}
int main()
{
cout << "Testing Student class\n";
Student student1;
student1.Set(1234, "John", 2.5);
student1.Print();
cout << "Testing Course class\n";
Course course(5);
course.Print();
course.Read();
course.Print();
course.TopStudents(3.5);
return 0;
}