私は
using namespace std;
typedef vector<Coil*> CoilVec;
CoilVec Coils;
とCoil
の基本クラスでCilCoil
ありRectCoil
、それぞれ円筒形コイルと長方形コイルです。ここで、指している incalcField
ごとにメンバー関数を呼び出したいと思います。このメンバー関数は、基本クラスでは純粋に仮想ですが、派生クラスで実装されており、その宣言は次のようになります。Coil
Coils
virtual TVector3 calcField(const TVector3&);
Root ライブラリのTVector3
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*]