I am getting quite a bit confused, to me I think the following two implementation are valid (but my syntax has error), but I can not get either one of them to work. This is a ye
Your Student
class has only one constructor, namely
Student(string myUID, string myLastName, string myFirstName,
string major, string age, string homeState, bool isWorking);
Because of this, the default constructor does not get generated by the compiler. Since you also don't provide one (default ctor: one callable with no arguments), the compiler cannot create the array -- it would need default-constructable objects to place in the elements (initialize the array with).
To fix, you will need to provide a default ctor:
Student();
the behavior of which you will need to find out yourself based on how Student
is assumed to behave.
You could also provide default arguments to all parameters of the listed Student
ctor, so it is callable without explicitly specifying any of the parameters -- thus qualifies as a default ctor
If a default ctor is not feasable, you will need to become more creative and dynamically allocate the array, copy all existing elements to the new one and assign the given new element to it when needed. Note that with this you are essentially re-creating the functionality of the std::vector
container. Note also, that this approach is really hard to get it right, so I advise against it, especially since you are just learning the basics.
Yet another approach is to create an array of pointers to Student
, so you can assign each as you create them dynamically. You will have to test whether the element is a null pointer, before you attempt to access the Student
object pointed to.
Student* students[20];
students[0] = new Student(/*list of actual parameter values*/);
if (student[0] != NULL) {
students[0]->getName(); // assuming you have a member function getName()
}
Note: the index 0 is used only for illustrative purposes, you can use a variable as well (as long as you ensure it stays within 0 and 19 (inclusive) -- note even though you have 20 elements, the largest index is 19 as the counting starts from 0
My question is why doesn't the method 1 work?
Because Student
does not have a default constructor (a constructor with no arguments). A default constructor is generated by the compiler only if no constructors are defined. As constructors have been defined, which take at least one argument no default constructor exists. When you declare the array:
Student arrayOfStudents[20];
Twenty instances of Student
are being created, using the default constructor which does not exist.
You could define a default constructor which would allow use of the array, but does it make sense for a Student
to exist without a name, age, etc?
Or, use an array of Student*
:
Student* arrayOfStudents[20] = {}; // The '{}' will set all pointers to NULL.
EDIT:
If the number of students is not known until runtime then:
// Get number of students somehow.
int numberOfStudents = ...;
// The '()' will set all pointers to NULL.
Student** arrayOfStudents = new Student*[numberOfStudents]();
Remember to delete
each entry in arrayOfStudents
and to delete[]
the arrayOfStudents
itself.