在每个函数中再次使用 class 对象时发生异常,未知信号错误

Exception has occured, unknown signal error when using class object again inside each function

我正在尝试为我注册的课程编写 C++ 代码,我在其中保留注册该课程的学生的信息。

我的问题是:

#include <iostream>
using namespace std;
#define MAX  10

class Student {
private:
   int ID;
   string name;
   string surname;

public:
   Student()
   {
      ID = 0;
      string name = "" ;
      string surname = "";

   }
   void setID(int ID_set);
   int getID();
   void setName(string name_set);
   string getName();
   void setSurName(string surname_set);
   string getSurName();
};

class Course {
   private:
      Student students[MAX];
      int num =0 ; // The current number of students in the course, initially 0.
      float weightQ;
      float weightHW;
      float weightF;

   public:
      Course()
      {
         students[num] = {};
         weightQ = 0.3;
         weightHW = 0.3;
         weightF = 0.4;

      }
      int getNum(); // Returns how many students are in the course
      void addNewStudent(Student new_student);
      void updateWeights(float weightQ_update, float weightHW_update, float weightF_update);
      void getStudent(int ID_given);
};

// Method declerations for the class Student

void Student :: setID(int ID_set){
   ID = ID_set;
}
int Student :: getID(){
   return ID;
}
void Student :: setName(string name_set){
   name = name_set;
}
string Student :: getName(){
   return name;
}
void Student :: setSurName(string surname_set){
   surname = surname_set;
}
string Student :: getSurName(){
   return surname;
}   

// Method declerations for the class Course
int Course :: getNum(){
   return num;
}
void Course :: addNewStudent(Student new_student){
   students[num] = new_student ;
   num = num + 1;
}
void Course :: updateWeights(float weightQ_update, float weightHW_update, float weightF_update){
            weightQ = weightQ_update;
            weightHW = weightHW_update;
            weightF = weightF_update; 
}
void Course :: getStudent(int ID_given){
   for(int i = 0; i<MAX; i++){
      if(ID_given == students[i].getID()){
         cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
        
      }
   } 
}

 void addNewStudent(int ID, string name, string surname){
    Student student;
    Course ECE101;
    student.setID(ID);
    student.setName(name);
    student.setSurName(surname);
    ECE101.addNewStudent(student);
   }
 void showStudent(int ID){
    Course ECE101;
    ECE101.getStudent(ID);
 }

int main(){
   Course ECE101;
   cout << "Welcome to the ECE101 Classroom Interface"<<"\n";
   cout << "Choose your option\n";
   string option_1 = "1) Add a student ";
   string option_2 = "2) Search a student by ID";
   cout << "Enter your option: ";
   int x;
   int ID;
   string name, surname;
   cin >> x;
   if (x == 1)
      cout << "Enter the student ID ";
      cin >> ID;
      cout << endl;
      cout << "Enter the student name ";
      cin >> name;
      cout << endl;
      cout << "Enter the student surname " ;
      cin >> surname;
      addNewStudent(ID, name, surname);

   return 0;
}

每次调用您的 addNewStudent 函数都会创建一门新课程。您可以将对课程的引用作为参数传递给函数并调用 Course.addNewStudent(student)。您需要确保在定义函数时将其指定为参考,否则您将只创建课程的副本。

为了使菜单更具交互性,您可以添加一个 do while 语句 来接受 3 个选项:

  • 注册
  • 显示数据
  • 退出
int main(){
   Course ECE101;
   int x;
   int ID;
   string name, surname;

   string option_1 = "1) Add a student\n";
   string option_2 = "2) Search a student by ID\n";

   cout << "Welcome to the ECE101 Classroom Interface\n";
   cout << "Choose your option\n";
   cout << option_1 << option_2;

   cin >> x;

    do {
        if (x == 1) {
            cout << "Enter the student ID ";
            cin >> ID;
            cout << endl;
            cout << "Enter the student name ";
            cin >> name;
            cout << endl;
            cout << "Enter the student surname " ;
            cin >> surname;
            addNewStudent(ID, name, surname, ECE101);
        }
        else {
            cout << "Enter the student ID\n";
            cin >> ID;
            showStudent(ID, ECE101);
        }
        cout << "Choose your option\n";
        cin >> x;
    } while(x != 3);

    return 0;
}

addnewStudent()showStudent() 方法现在接受 Course 的实例作为参数,以便能够添加 students.

void addNewStudent(int ID, string name, string surname, Course &course) {
    Student student;
    student.setID(ID);
    student.setName(name);
    student.setSurName(surname);
    course.addNewStudent(student);
}

void showStudent(int ID, Course &course) {
    course.getStudent(ID, course);
}

该函数也是从 class 修改而来。

void Course::getStudent(int ID_given, Course &course) {
   for(int i = 0; i<MAX; i++){
      if(ID_given == students[i].getID()){
         cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
       }
   } 
}

Demo