在另一个类中使用一个类的对象

Using an object of a class in another class

本文关键字:一个 对象 另一个      更新时间:2023-10-16

我有一个名为Student的类,它看起来像这个

class Student
{   string name;
    unsigned long int ID ;
    string email;
    unsigned short int year;
    public : 
         Student() // Constructor
         string getName(void);
         unsigned long int getID(void);
         string getEmail(void);
         unsigned short int getYear(void);   
{

以及另一个名为eClass 的类

class eClass
 {
     string eclass_name;
     Student* students[100];
     unsigned int student_count;
     public:
        eClass(string name)
        {
            student_count=0 ; 
            eclass_name = name  ; 
        }
        void add(Student& obj)
        {
            bool res = exists(Student obj);  ****
            if (res)
            {
            }
            else
            {
                students[student_count] = obj ;  **** 
                student_count++ ; 
            }
        }
        bool exists(Student &obj)
        {
            unsigned long int code = obj.getID(); ****
            bool flag = FALSE ;
            for (int i = 0 ; i<=student_count ; i++ )
            {
                unsigned long int st = students[i]->getID();
                if (code==st)
                {
                    flag = TRUE;
                }
            }
            return flag;
        }
    };

它基本上创建了一个表示课程的对象,然后在检查学生是否已经属于该课程后,通过add()将学生添加到该课程中。

我用***标记的行出现错误。有人能帮我出什么问题吗。。。我不太确定我是否理解如何在另一个类中使用类的对象。

这是不正确的:

       bool res = exists(Student obj);  // ****

它应该是这样的:

       bool res = exists(obj);  // ****

obj是可在函数内部使用的函数(类型为Student)的参数。在这一行中,您正在使用该参数将其传递给另一个函数。

相关文章: