So i'm trying to create a private member array on my School class where i'm going to be holding the address of some student objects. My goal is to simulate the stusents entering the school building if the capacity of the building allows as such (think of the problem with small numbers) I have managed to implement the "address object part" but i have an issue concerning the size of the array. The variable which gives the size of the array is also a private member of the same class. That being said the compiler gives the error:
error: invalid use of non-static data member ‘School::class_capacity’
148 | Student* pointer_array[class_capacity];
Here's School class:
class School
{
private:
int class_capacity;
Student* pointer_array[class_capacity];
public:
School(const int capacity)//constructor
:class_capacity(capacity)
{
cout << "A New School has been created!" << endl;
};
~School(){//destructor
cout << "A School to be destroyed!" << endl;
};
void enter(Student* student, int stc=0/*student counter*/);
}
Student class is :
class Student
{
private:
string name;
int no_floor;
int no_classroom;
public:
Student(const string& nam,int no_fl,int no_cla)//constructor
: name(nam), no_floor(no_fl), no_classroom(no_cla)
{
cout << "A new student has been created! with name " << name << " heading to floor: "<< no_floor << " class: " << no_classroom << endl;
};
~Student()//destructor
{
cout << "A Student to be destroyed! with name " << name << " is at "<< " class: " << no_classroom;
};
Lastly my main where i allocate an array of pointers to Student objects.
int main(void)
{
//Student creation
int i,floor,classroom;
string stname;
Student* students[5];
for(i=0; i<5; i++)
{
cin >> stname;
cin >> floor;
cin >> classroom;
students[i] = new Student(stname, floor, classroom);
}
for(i=0; i<5; i++)
{
delete students[i];
}
}
And the enter function code:
void School::enter(Student* student, int stc/*student counter*/)
{
pointer_array[stc] = student;
(pointer_array[stc])->print();
cout << " enters school!" << endl;
}
The array i'm trying to create in School class is meant to keep a pointer to already created Student objects on my main.
Does anybody know how to solve this? Again library usage is forbidden