I recently spent quite some time understanding the error message when calling func()
in this piece of code:
int main()
{
vector< vector<double> > v;
double sum = 0;
for_each( v.begin(), v.end(),
bind2nd( ptr_fun(func), &sum ) );
return 0;
}
when func()
was declared like so, the code compiled fine:
void func( vector<double> v, double *sum )
{
}
when I used this declaration (for efficiency), I got a compiler error:
void func( const vector<double> &v, double *sum )
{
}
The error I expected to see was something like a reference-to-reference error, because of the definition of operator() of binder2nd,
result_type operator()(const argument_type& _Left) const
Instead, to my surprise, the error the Visual C++ (VS2012) compiler gave me was:
error C2535: 'void std::binder2nd<_Fn2>::operator ()(const std::vector<_Ty> &) const' : member function already defined or declared
which I cannot decipher.
- Can you explain under which mechanism the
operator()
is already defined?
The full error I got was:
error C2535: 'void std::binder2nd<_Fn2>::operator ()(const std::vector<_Ty> &) const' : member function already defined or declared
with
[
_Fn2=std::pointer_to_binary_function<const std::vector<double> &,double *,void,void (__cdecl *)(const std::vector<double> &,double *)>,
_Ty=double
]
c:\vc\include\xfunctional(319) : see declaration of 'std::binder2nd<_Fn2>::operator ()'
with
[
_Fn2=std::pointer_to_binary_function<const std::vector<double> &,double *,void,void (__cdecl *)(const std::vector<double> &,double *)>
]
c:\consoleapplication1.cpp(31) : see reference to class template instantiation 'std::binder2nd<_Fn2>' being compiled
with
[
_Fn2=std::pointer_to_binary_function<const std::vector<double> &,double *,void,void (__cdecl *)(const std::vector<double> &,double *)>
]
Build FAILED.