我最近花了相当长的一段时间,这段代码调用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的)的定义,
result_type operator()(const argument_type& _Left) const
相反,我惊讶的是, Visual C++(VS2012)编译器给我的错误是:
error C2535: 'void std::binder2nd<_Fn2>::operator()(const std::vector<_Ty> &) const' : member function already defined or declared
我不能解密。
- 你能解释下其机制
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.
由于您使用VS 2012,我认为您可以切换到C++ 11并使用lambda/std :: bind来避免这些弃用的东西。 – kennytm
看起来像它指的是binder2nd结构的op(),由于参考折叠(或类似的)被定义了两次相同的签名。 – PlasmaHH
这很有趣。看起来你不能在旧的活页夹包装中使用引用参数类型?! –