I'm going through Paul Deitel's C++ fundamentals video course, and encountered a small scoping issue. Below is my GradeBook Class code. I left out the main cpp, as all it really does is initialize a grades array with 10 numbers, and calls the processGrades function in the class below.
I usually like to put my private variables section of a class first, so I can see them easily, but when I did that, I got a lot of scoping errors (i.e. "Grade not declared in scopre," "Numbers not declared in scope," etc.). Took me a while to figure out that placing private under public got rid of those scoping issues, and I just want to know why, so I can understand what I did wrong.
I'm going to re-post this code because to read it, I had to re-format it. Indenting is incredibly important because otherwise you can't see your structures. I couldn't tell where one function started and the other ended:
This code was able to compile. Was this an example of the working or not working code? I see that your two members: grades[] and courseName are both private here.
There are no error messages, he specifically stated that this is the code that compiled.
leo255 wrote:
Took me a while to figure out that placing private under public got rid of those scoping issues, and I just want to know why, so I can understand what I did wrong.
There are no error messages, he specifically stated that this is the code that compiled.
leo255 wrote:
Took me a while to figure out that placing private under public got rid of those scoping issues, and I just want to know why, so I can understand what I did wrong.
Does it mean that we shall guess what is the problem?
I think the problem is that when you exchange the public and private sections variable students becomes undefined for array grades. The order of data member declarations is important. So variable students shall be defined before grades[students]