1

I have a class named Student which looks like this

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);   
{

and another class named 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;
        }
    };

It basically creates an object which represents a lesson and then adds students to the lesson via add() after it checks that the student does not already belong to the lesson.

I am getting an error at the lines I've marked with ****. Could someone help me what's going wrong...I'm not pretty sure I've understood how to use an object of a class in another one.

1 Answer 1

1

This isn't correct:

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

It should look like this:

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

obj is the argument to the function (of type Student) that can be used inside the function. On this line, you are using that argument to pass it to another function.

Sign up to request clarification or add additional context in comments.

10 Comments

Also Student* students[100]; should be Student students[100];, or, if not, proper memory allocation has to be done; students[student_count] = obj won't compile.
@vsoftco you can do Student* students[100], just use Students[student_count] = new student(), every time you want to add an object. That way you don't allocate all of them at once. Just the ones you have.
@lciamp You suggest using Students[student_count] = new student() every time I want to add an object of type student to the Students table?
@Alice1nw0 You have to decide whether you want to use an array of pointers or not. For a simple example it's easier to avoid them and do as vsoftco has suggested - do without them and use an array of objects instead of an array of pointers to objects.
The guidelines we were given suggest using Student* students[500];... I can't really understand what this is supposed to mean...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.