func()
我最近在这段代码中调用时花了一些时间来理解错误消息:
int main()
{
vector< vector<double> > v;
double sum = 0;
for_each( v.begin(), v.end(),
bind2nd( ptr_fun(func), &sum ) );
return 0;
}
当func()
像这样声明时,代码编译得很好:
void func( vector<double> v, double *sum )
{
}
当我使用这个声明(为了提高效率)时,我得到了一个编译器错误:
void func( const vector<double> &v, double *sum )
{
}
由于 binder2nd 的 operator() 的定义,我希望看到的错误类似于引用到引用的错误,
result_type operator()(const argument_type& _Left) const
相反,令我惊讶的是,Visual C++ (VS2012) 编译器给我的错误是:
错误 C2535:'void std::binder2nd<_Fn2>::operator ()(const std::vector<_Ty> &) const':成员函数已定义或声明
我无法破译。
- 你能解释一下已经定义的
operator()
机制吗?
我得到的完整错误是:
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.