我有一个
using namespace std;
typedef vector<Coil*> CoilVec;
CoilVec Coils;
是和Coil
的基类CilCoil
,RectCoil
分别是圆柱形线圈和矩形线圈。现在我希望calcField
在每个Coil
指向 in上调用一个成员函数Coils
。该成员函数在基类中是纯虚函数,但已在派生类中实现,其声明如下所示:
virtual TVector3 calcField(const TVector3&);
是TVector3
来自 Root 库的 3D 矢量类。现在的想法是计算每个输入的字段Coil
并将Coils
它们加在一起。由于每次调用的参数calcField
(即计算字段的位置的向量)都是相同的,我想使用<algorithm>
or<numeric>
标头中的 STL 算法来做这样的事情(想象):
using namespace std;
typedef vector<Coil*>::const_iterator CoilIt;
const TVector3& P(1.,1.,1.); // Let's say we want to know the total field in (1,1,1)
TVector3 B; // Default initialization: (0,0,0)
CoilIt begin = Coils.begin();
CoilIt end = Coils.end();
B = accumulate(begin, end, B, bind2nd(mem_fun(&Coil::calcField), P));
显然,既然我是来问一个问题的,这似乎不起作用。所以我的问题很简单:为什么这不起作用和/或你将如何以正确的方式去做(在 STL 的范围内)?
我在尝试编译上述文件时收到以下错误消息(我正在使用的文件称为 Interface.cpp,它是第三方代码):
In file included from /usr/include/c++/4.5/numeric:62:0,
from Interface.cpp:7: /usr/include/c++/4.5/bits/stl_numeric.h: In function ‘_Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation) [with _InputIterator = __gnu_cxx::__normal_iterator<Coil* const*, std::vector<Coil*> >, _Tp = TVector3, _BinaryOperation = std::binder2nd<std::mem_fun1_t<TVector3, Coil, const TVector3&> >]’:
Interface.cpp:289:72: instantiated from here
/usr/include/c++/4.5/bits/stl_numeric.h:150:2: error: no match for call to ‘(std::binder2nd<std::mem_fun1_t<TVector3, Coil, const TVector3&> >) (TVector3&, Coil* const&)’
/usr/include/c++/4.5/backward/binders.h:147:7: note: candidates are: typename _Operation::result_type std::binder2nd<_Operation>::operator()(const typename _Operation::first_argument_type&) const [with _Operation = std::mem_fun1_t<TVector3, Coil, const TVector3&>, typename _Operation::result_type = TVector3, typename _Operation::first_argument_type = Coil*]
/usr/include/c++/4.5/backward/binders.h:153:7: note: typename _Operation::result_type std::binder2nd<_Operation>::operator()(typename _Operation::first_argument_type&) const [with _Operation = std::mem_fun1_t<TVector3, Coil, const TVector3&>, typename _Operation::result_type = TVector3, typename _Operation::first_argument_type = Coil*]