std::accumulate error

Apr 24, 2012 at 3:14pm
Hello there,

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]

StutendsInfo.h
1
2
3
4
5
6
7
8
9
10
11
12
#include <vector>
typedef std::vector<float> Hw;
class StudentsInfo
{
public:

	Hw getHw()const  {return _homework;}

private:
	Hw _homework;
	
};


Caclulate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "StudentsInfo.h"
#include <list>

typedef std::list<StudentsInfo> Records;

class Calculate
{
public:
	
        static float grade(float mid,float final, float hw);
	static float averageTotalGrade(const  StudentsInfo& s );
	static float averageGradeHandler(const StudentsInfo& s);

};


Caclulate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Calculate.h"
#include <numeric>
float Calculate::grade( float mid,float final, float hw )
{
	return (0.2*mid) + (0.4 * final) + (0.4 * hw);
}
float Calculate::averageTotalGrade(const  StudentsInfo& s )
{
	return std::accumulate(s.getHw().begin(),s.getHw().end(),0.0)/ s.getHw().size();
}

float Calculate::averageGradeHandler(const  StudentsInfo& s )
{
	return grade(s.getMid(),s.getFinal(),averageTotalGrade(s));
}



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.
Last edited on Apr 24, 2012 at 3:24pm
Apr 24, 2012 at 3:21pm
Your class StudentsInfo has no methods begin() and end(). So you shall rewrite function averageTotalGrade the following way

float Calculate::averageTotalGrade(const StudentsInfo& s )
{
return std::accumulate(s.getHw().begin(),s.getHw().end(),0.0)/ s.getHw().size();
}

Last edited on Apr 24, 2012 at 3:24pm
Apr 24, 2012 at 3:26pm
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.
Last edited on Apr 24, 2012 at 3:33pm
Apr 24, 2012 at 4:20pm
What is the run-time error? Maybe your vector is empty.
Apr 24, 2012 at 4:31pm
it say vectoriterator not compatble , and well i am sure vector isnt empty have checked that.
Apr 24, 2012 at 5:22pm
You should show the code where you call this function. The presented code is valid, but the error occurs as a consequence of the function call.
Apr 24, 2012 at 7:09pm
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? 
Last edited on Apr 24, 2012 at 9:56pm
Apr 24, 2012 at 7:40pm
@clanmjc ,

good remark. I think that getter function returns a reference to the vector.
Apr 25, 2012 at 1:17pm
@clanmjc ,

Didn't think of that , thanks for help :)
Topic archived. No new replies allowed.