Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用bind2nd()时出现奇怪的编译器错误:“1”;成员函数已定义或声明";而不是",;参考参考资料;_C++_Templates_Compiler Errors_Bind2nd - Fatal编程技术网

C++ 使用bind2nd()时出现奇怪的编译器错误:“1”;成员函数已定义或声明";而不是",;参考参考资料;

C++ 使用bind2nd()时出现奇怪的编译器错误:“1”;成员函数已定义或声明";而不是",;参考参考资料;,c++,templates,compiler-errors,bind2nd,C++,Templates,Compiler Errors,Bind2nd,我最近花了相当长的时间来理解在这段代码中调用func()时的错误消息: int main() { vector< vector<double> > v; double sum = 0; for_each( v.begin(), v.end(), bind2nd( ptr_fun(func), &sum ) ); return 0; } 当我使用此声明时(为了提高效率),我遇到了一个编译器错误: void fu

我最近花了相当长的时间来理解在这段代码中调用
func()
时的错误消息:

int main()
{
    vector< vector<double> > v;

    double sum = 0;
    for_each( v.begin(), v.end(), 
        bind2nd( ptr_fun(func), &sum ) );

    return 0;
}
当我使用此声明时(为了提高效率),我遇到了一个编译器错误:

void func( const vector<double> &v, double *sum )
{
}
但是,让我吃惊的是,Visual C++(VS2012)编译器给我的错误是:

错误C2535:'void std::binder2nd::operator()(const std::vector&)const':成员函数已定义或 宣布

我无法破译

  • 你能解释一下
    操作符()
    是在哪种机制下运行的吗 定义
我得到的全部错误是:

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.
错误C2535:'void std::binder2nd::operator()(const std::vector&)const':成员函数已定义或声明
具有
[
_Fn2=std::指向二进制函数的指针,
_Ty=双
]
c:\vc\include\xfunctional(319):请参见“std::binder2nd::operator()”的声明
具有
[
_Fn2=std::指向二进制函数的指针
] 
c:\consoleapplication1.cpp(31):请参阅对正在编译的类模板实例化“std::binder2nd”的引用
具有
[
_Fn2=std::指向二进制函数的指针
]
生成失败。

这不是答案,但我只想记录一下现代C++11解决方案,其中所有小型绑定帮助程序都被弃用,转而支持universal
std::bind

#include <functional>
#include <vector>
#include <algorithm>

void func(std::vector<double> const & v, double * sum) { /* ... */ }

int main()
{
    std::vector<std::vector<double>> v;
    double sum = 0;
    std::for_each(v.begin(), v.end(), std::bind(func, std::placeholders::_1, &sum));
}
#包括
#包括
#包括
void func(std::vector const&v,double*sum){/*…*/}
int main()
{
std::向量v;
双和=0;
std::for_each(v.begin(),v.end(),std::bind(func,std::占位符::_1,&sum));
}

< C++ > 11的可变模板,以及更全面的类型修改特征集合,给出 STD::BIN < /code >比 中的以前的组件更强的演绎能力。

< P>此行为定义良好(每个正确的C++编译器将无法编译您的代码)。 从类模板
binder2nd
上的标准(N3376)部分
D.9.3
中,存在
操作符()
的这两个定义:

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;

如果
第一个参数类型
已经是一个
常量&
,那么它们将是冲突的。

因为您使用的是VS 2012,我认为您可以切换到C++11并使用lambdas/
std::bind
来避免这些不推荐的内容。看起来它引用的是binder2nd结构的op(),这是由于引用折叠(或类似)造成的使用相同的签名定义了两次。这很有趣。在旧的活页夹包装中似乎不能有引用参数类型+1,如果
第一个参数类型
已经是
T&
,这似乎是一个问题?至少我发现,
std::bind2nd(std::ptr_-fun(f),“xyz”)f
void f(std::ostream&str,const char*s),则code>无法编译,并出现完全相同的错误。唉,按值传递
std::ostream
不是一个选项,因此您似乎必须传递指向它的指针-/
#include <functional>
#include <vector>
#include <algorithm>

void func(std::vector<double> const & v, double * sum) { /* ... */ }

int main()
{
    std::vector<std::vector<double>> v;
    double sum = 0;
    std::for_each(v.begin(), v.end(), std::bind(func, std::placeholders::_1, &sum));
}
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;