I have been getting a run-time error in the part of code i will be pasting down there. I know how to fix the error but actually i wanted to know why that error comes up?! :)
My code is like that:[will try to inlcude only the partes that is of relevance to the error]
After that when I call the method Calculate::averageGradeHandler()
I get a run-time error, which doesnt occur when i try to remove the std::accumulate nor with the other fix of passing:
(const Hw&)
as argument to
Calculate::averageTotalGrade()
instead of
(const StudentsInfo& s) of course with changes needed for the new argument.
ye that is what i have, i think i copied the fixed one instead of that. And yes this line is the part where the error comes from. So when this line is time to execute i get the rintime error.
The vector iterator is not compatible because when you call... s.getHw() you are returning a copy of the vector (which I would advise against because that is very expensive as the container gets larger).
Ok so it makes a copy so what? Well then you call s.getHw() again to get the end(), well this copy is totally independent from the other copy you just got back, thus the iterators are not compatible (because they are not from the same container).
If you change your student info function to return a reference, then you could do what you are trying to do.
Hw& getHw()const {return _homework;}
in otherwords since you return a copy and not a reference:
1 2
s.getHw().begin(); //This iterator is not from the same container as
s.getHw().end(); //This iterator is from another copy of the container.
Broken down it is basically this:
1 2 3
Records record1 = s.getHw(); //container 1
Records record2 = s.getHw(); //container 2
//You wouldn't compare the begin and end iterators from different containers to control a loop would you?