2016-10-04 62 views
0

我使用OpenCV庫與C++,我試圖來計算它們包含在vector<Point2f> difference用法積累了自定義對象

點類有是float x屬性點的總和。

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

我定義了上面的函數,並從下面顯示的累積中調用它。但它會引發錯誤。

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

錯誤是:

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

注:我使用C++ 11

回答

2
float pointSumX(Point2f pt1, Point2f pt2) 

應該是

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

作爲lhs是累加器。

還要注意的是,你應該把它

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

返回float而不是int

+0

謝謝。它現在有效。在函數中const和&的原因是什麼? – Bhoke

+2

@Bhoke:避免複製:通過const引用而不是按值傳遞。 (對於'Point2f',由於類很小,所以它可能會有爭議,但默認情況下是通過const引用而不是按值傳遞對象)。 – Jarod42