I am trying to write a function that acts on a member of a struct that is in an array of structs that is a member of another struct :). The first thing I need to do is find out the length of the array of structs, but I keep getting the error expression must be class type.
What would be the appropriate way for me to get the length of this array? (PS. The function has to take the Student structure as an argument.)
The structs:
struct Class
{
string title;
int units;
char grade;
};
struct Student
{
string name;
double gpa;
Class classes[500];
};
My function looks something like this:
void gpaCalculate (Student s)
{
int size = s.classes.size() ;
//Lots of awesome code
}
The code
indicates that a
Studentcan attend to maximum 500 classes, and that this is some school assignment where you are expected/required to use raw arrays, instead of the otherwise much more appropriatestd::vector.First, the magic number 500 should be made a constant, like so:
Then in each
Studentobject you need to store the actual number of classes this student attends to:The constructor ensures that
gpaandnClasseswill be zeroed.Since they’re of built-in types that’s otherwise not guaranteed (per the principle that you don’t pay for what you don’t use, so it’s your decision).