为什么我在课程课上遇到“TopStudents”方法的错误? 我是C++新手,现在正在尝试通过添加一个名为TopStask的方法来扩展课程类,该方法旨在搜索学生对象数组,以找到和打印所有GPA的学生,这些值高于给定的值,这些值可以在主程序中找到。我已经标记了我遇到错误的行
编译此程序时,我会收到以下错误消息: --错误:“int”之前应为主表达式 学生[index].Getint&uaid,string&name,float&gpa; ^~~ --错误:应在“&”之前使用主表达式 学生[index].Getint&uaid,string&name,float&gpa; ^ --错误:“名称”未在此范围中声明 学生[index].Getint&uaid,string&name,float&gpa; ^~~~ --错误:“float”之前应为主表达式 学生[index].Getint&uaid,string&name,float&gpa; ^~~~~ --错误:“gpa”未在此作用域中声明 如果gpa>=minGpa{ ^~~ 这是我正在使用的输入: 1234苏珊3.9 2345约翰3.2 3456劳拉3.8 4567布莱恩3.5 5678大卫3.1 最后,这是我的代码:为什么我在课程课上遇到“TopStudents”方法的错误? 我是C++新手,现在正在尝试通过添加一个名为TopStask的方法来扩展课程类,该方法旨在搜索学生对象数组,以找到和打印所有GPA的学生,这些值高于给定的值,这些值可以在主程序中找到。我已经标记了我遇到错误的行,c++,arrays,class,C++,Arrays,Class,编译此程序时,我会收到以下错误消息: --错误:“int”之前应为主表达式 学生[index].Getint&uaid,string&name,float&gpa; ^~~ --错误:应在“&”之前使用主表达式 学生[index].Getint&uaid,string&name,float&gpa; ^ --错误:“名称”未在此范围中声明 学生[index].Getint&uaid,string&name,float&gpa; ^~~~ --错误:“float”之前应为主表达式 学生[index
////////////////////////////
///////////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;
}
不能只在调用函数时声明新变量 你想要的可能是这样的:
int uaid;
string name;
float gpa;
students[index].Get(uaid, name, gpa);
@drescherjm这是不够的,因为uaid、名称和gpa实际上是输出参数。我不知道为什么Get是这样设置的,但确实如此,所以在Course或Course::TopStudents中的某个地方,您需要声明一些变量,这些变量实际上可以用作Get的参数。