Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of accumulate with a custom object

Tags:

c++

c++11

opencv

I am using opencv library with C++, and I am trying to compute sum of points which are contained in vector<Point2f> difference

Point class has x property which is float.

float pointSumX(Point2f pt1,Point2f pt2)
{
    return (pt1.x + pt2.x);
}

I defined function as above, and call it from accumulate shown below. But it throws error.

float avgMotionX = accumulate(difference.begin(),difference.end(),0,pointSumX);

Error is:

error: could not convert ‘__init’ from ‘int’ to ‘cv::Point_’ __init = __binary_op(__init, *__first);

Note: I am using C++11

like image 783
Bhoke Avatar asked Oct 23 '25 13:10

Bhoke


1 Answers

float pointSumX(Point2f pt1, Point2f pt2)

should be

float pointSumX(float lhs, const Point2f& rhs)
{
    return lhs + rhs.x;
}

as lhs is the accumulator.

Note also that you should call it

std::accumulate(difference.begin(), difference.end(), 0.f, pointSumX); // 0.f instead of 0

to return float and not int.

like image 67
Jarod42 Avatar answered Oct 26 '25 02:10

Jarod42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!