为啥我在课程课程中收到方法“TopStudents”的错误? [关闭]

Posted

技术标签:

【中文标题】为啥我在课程课程中收到方法“TopStudents”的错误? [关闭]【英文标题】:Why am I getting an error for the method "TopStudents" in the course class? [closed]为什么我在课程课程中收到方法“TopStudents”的错误? [关闭] 【发布时间】:2020-12-02 00:18:15 【问题描述】:

我是 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;

【问题讨论】:

@drescherjm 这还不够,因为uaidnamegpa实际上是输出参数。我不知道为什么Get 是这样设置的,但确实如此,所以在CourseCourse::TopStudents 的某个地方,您需要声明一些实际上可以用作Get 的参数的变量。跨度> 【参考方案1】:

您不能只在调用函数时声明新变量。

你想要的大概是这样的:

int uaid;
string name;
float gpa;
students[index].Get(uaid, name, gpa);

【讨论】:

以上是关于为啥我在课程课程中收到方法“TopStudents”的错误? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

为啥我在 Eclipse 中看到两次相同的课程

为啥我收到“表达式不可分配”错误?

Express REST API - 删除方法

您何时在 Objectify for GAE 中注册课程?

错误 C2143。为啥 VC++ 找不到我的课程?

为啥 sklearn Imputer 需要适配?