I have a
using namespace std;
typedef vector<Coil*> CoilVec;
CoilVec Coils;
with Coil
being a base class for CilCoil
and RectCoil
, a cilindrical coil and a rectangular coil, respectively. Now I wish to invoke a member function calcField
on each Coil
pointed to in Coils
. This member function is purely virtual in the base class but has been implemented in the derived classes and its declaration looks like this:
virtual TVector3 calcField(const TVector3&);
with TVector3
being the 3D vector class from the RooT library. The idea now is to calculate the field of every Coil
in Coils
and add them together. Since the argument of calcField
(namely a vector to the position in which to calculate the field) would be the same for every call, I would like to use an STL algorithm from the <algorithm>
or <numeric>
header to do something like this (imagined):
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));
Obviously, since I'm here to ask a question, this doesn't seem to work. So my question is quite simply put: why does this not work and/or how would you go about doing it the right way (within the limits of the STL)?
I get the following error messages trying to compile the above (the file I'm working in is called Interface.cpp, it's third-party code):
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*]