因此,我试图在std :: vector上实现一个仿函数,以对两个Point组件之一/两者执行std :: accumulate。
uint32_t EllipseFit::getSum(SampleSumTypes method)
{
uint32_t sum=0;
/* determine which Sum function to use */
switch(method)
{
case SUM_X:
sum = accumulate(m_dataPoints.begin(), m_dataPoints.end(), 0, SumX);
break;
case SUM_Y:
sum = accumulate(m_dataPoints.begin(), m_dataPoints.end(), 0,
[&](const Point &p) {sum += p.Y();} );
break;
/* similar case statements... */
}
return sum;
}
因此,我正在尝试两种方法来执行“混合”累加器功能
/* defined a little earlier in the *.cpp file, with global storage */
struct F1
{
uint32_t operator() (uint32_t sum, const Point &p)
{
return sum + p.X();
}
} SumX;
在两种情况下,我都得到此错误输出:
In file included from /usr/include/c++/5/algorithm:62:0,
from ellipse-fitting.cpp:17:
/usr/include/c++/5/bits/stl_algo.h: In instantiation of ‘_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = __gnu_cxx::__normal_iterator<Recognition::Point*, std::vector<Recognition::Point> >; _OIter = __gnu_cxx::__normal_iterator<Recognition::Point*, std::vector<Recognition::Point> >; _UnaryOperation = Recognition::EllipseFit::calcEllipse()::<lambda(Recognition::Point&)>]’:
ellipse-fitting.cpp:168:60: required from here
/usr/include/c++/5/bits/stl_algo.h:4175:12: error: no match for ‘operator=’ (operand types are ‘Recognition::Point’ and ‘void’)
*__result = __unary_op(*__first);
^
In file included from /media/sf_shared-data/src/ml-algorithm/cpp/libimgproc/include/libimgproc.hpp:22:0,
from ellipse-fitting.hpp:17,
from ellipse-fitting.cpp:19:
/media/sf_shared-data/src/ml-algorithm/cpp/libimgproc/include/rec_types.hpp:39:11: note: candidate: Recognition::Point& Recognition::Point::operator=(const Recognition::Point&)
class Point
^
/media/sf_shared-data/src/ml-algorithm/cpp/libimgproc/include/rec_types.hpp:39:11: note: no known conversion for argument 1 from ‘void’ to ‘const Recognition::Point&’
/media/sf_shared-data/src/ml-algorithm/cpp/libimgproc/include/rec_types.hpp:39:11: note: candidate: Recognition::Point& Recognition::Point::operator=(Recognition::Point&&)
/media/sf_shared-data/src/ml-algorithm/cpp/libimgproc/include/rec_types.hpp:39:11: note: no known conversion for argument 1 from ‘void’ to ‘Recognition::Point&&’
Makefile:56: recipe for target 'ellipse-fitting.o' failed
make[1]: *** [ellipse-fitting.o] Error 1
make[1]: Leaving directory '/media/sf_shared-data/src/ml-algorithm/cpp/libimgproc/algorithms/ellipse-fit'
Makefile:39: recipe for target 'algorithms/ellipse-fit' failed
我的猜测是我的'Point'类缺少一些东西来放置编译器,但我很困惑。
这是我的积分班:
class Point
{
protected:
int16_t px;
int16_t py;
public:
Point(int16_t x=SHRT_MIN, int16_t y=SHRT_MIN);
int16_t &X();
int16_t X() const;
int16_t &Y();
int16_t Y() const;
operator bool() const;
bool operator !() const;
bool operator != (const Point &pt2) const;
bool operator < (const Point &pt2) const;
Point &operator -= (const Point &p2);
double operator *(const Point &p2) const;
bool horizontalTo(Point &pt2);
bool verticalTo(Point &pt2);
double dot(Point &pt2);
friend std::ostream &operator << (std::ostream &out, Point &p);
};
double EllipseFit::getMeanX()
{
double mean = (double)getSum(SUM_X);
return mean / m_dataPoints.size();
}
double EllipseFit::getMeanY()
{
double mean = (double)getSum(SUM_Y);
return mean / m_dataPoints.size();
}
Ellipse EllipseFit::calcEllipse()
{
uint16_t meanX=(uint16_t)getMeanX(), meanY=(uint16_t)getMeanY();
uint32_t A, B, C, D, E, F, G;
/* shift the points into (U, V) points := {X - mean(X), Y - mean(Y)} */
transform(m_dataPoints.begin(), m_dataPoints.end(), m_dataPoints.begin(), \
[&](Point &p) {p -= Point(meanX, meanY); } );
A=getSum(this->SUM_XX);
B=getSum(this->SUM_XY);
C=getSum(this->SUM_YY);
D=getSum(this->SUM_XXX);
E=getSum(this->SUM_XYY);
F=getSum(this->SUM_YYY);
G=getSum(this->SUM_XXY);
return Ellipse(A, B, C, D, E, F, G, m_dataPoints.size(), meanX, meanY);
}
谢谢您的建议!查尔斯
感谢Igor向我指出错误!转换点时,我忘记添加return语句。