奇怪的编译器错误使用bind2nd():“已定义或声明的成员函数”而不是“参考”。 [英] weird compiler error using bind2nd(): "member function already defined or declared" instead of "reference to reference"
问题描述
我最近在这段代码中调用 func()
时花了很多时间来理解错误信息:
int main()
{
vector< vector< double> > v;
double sum = 0;
for_each(v.begin(),v.end(),
bind2nd(ptr_fun(func),& sum));
return 0; $ c $ b}
>被声明如此,代码编译得很好:
void func(vector< double> v,double * sum)
{
}
当我使用这个声明编译器错误:
void func(const vector< double>& v,double * sum)
{
}
我希望看到的错误类似于 reference-to-reference 错误,因为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()
是什么机制已经
<$> p $ p>
错误C2535:'void std :: binder2nd< _Fn2> :: operator()(const std :: vector< _Ty> &)const':已定义或声明的成员函数
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):请参见声明'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):参见类模板实例化'std :: binder2nd< _Fn2>'被编译
with
[
_Fn2 = std :: pointer_to_binary_function< const std :: vector< double> &,double *,void,void(__cdecl *)(const std :: vector< double>&,double *)&
]
构建失败。
从类模板的标准(N3376)部分 D.9.3
code> binder2nd ,这两个定义 operator()
存在:
typename Fn :: result_type
operator()(const typename Fn :: first_argument_type& x)const;
typename Fn :: result_type
operator()(typename Fn :: first_argument_type& x)const;
如果 first_argument_type
$ c> const T& ,那么它们就会发生冲突。
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.
This behavior is well-defined (every correct C++ compiler will fail to compile your code).
From the standard (N3376) section D.9.3
on class template binder2nd
, these two defintions of operator()
exist:
typename Fn::result_type
operator()(const typename Fn::first_argument_type& x) const;
typename Fn::result_type
operator()(typename Fn::first_argument_type& x) const;
If first_argument_type
is already a const T&
, then they will be conflicting.
这篇关于奇怪的编译器错误使用bind2nd():“已定义或声明的成员函数”而不是“参考”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!