ファンクターを STL アルゴリズムと一緒に使用して、2 つのベクトルの内積を計算する方法を学んでいます。ここに私のコードがあります:
template<size_t DIM>
double Vector<DIM>::operator*(const Vector<DIM>& rhs) const {
double dotPro = 0;
std::for_each(vec, vec + DIM, std::bind2nd(dot_product<double>(rhs.get()), dotPro));
return dotPro;
}
/*vec is a double array and the data member of Vector class. I want to get the
dot product of rhs and *this by using std::for_each().
rhs.get()returns a const double* which is the start address of rhs's vec*/
/*The codes below define the functor. dotPro is passed as a reference so as to
it could be save the last result.*/
template<typename T>
struct dot_product: public std::binary_function<T, T, void> {
const T* arg;
sum(const T* dbl) : arg(dbl){};
void operator() (const T dbl, T& dotPro) {
dotPro += *arg++ * dbl;
}
};
申し訳ありませんが、質問を忘れてしまいました...質問は、私のコードがコンパイルされないことです。コンパイルエラーは次のとおりです。
error: no match for call to '(const dot_product<double>) (const double&, const double&)'|
note: candidates are: void dot_product<T>::operator()(T, T&) [with T = double]|
そして、これがbinders.hで発生するエラーです
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\backward\binders.h|147|error: return-statement with a value, in function returning 'void'|