Boost :: bind和std :: copy

Dan*_*ook 3 c++ boost bind

我正在尝试使用Boost :: bind和std :: copy来打印列表列表中的值.显然,我可以使用循环,为了清晰起见,我最终可能会这样做,但我仍然想知道我在这里做错了什么.

这是我的代码的提炼版本:

#include <boost/bind.hpp>
#include <iterator>
#include <algorithm>
#include <list>
#include <iostream>
using namespace std;
using namespace boost;

int main(int argc, char **argv){
list<int> a;
a.push_back(1);

list< list<int> > a_list;
a_list.push_back(a);

ostream_iterator<int> int_output(cout,"\n");

for_each(a_list.begin(),a_list.end(),
  bind(copy,
    bind<list<int>::iterator>(&list<int>::begin,_1),
    bind<list<int>::iterator>(&list<int>::end,_1),
    ref(int_output)
  ) //compiler error at this line
);
return 0;
Run Code Online (Sandbox Code Playgroud)

}

编译器错误开始

error: no matching function call to bind(<unresolved overloaded function type> .....
Run Code Online (Sandbox Code Playgroud)

我认为这意味着bind无法弄清楚最外层绑定的返回类型应该是什么.我不怪它,因为我也不能.有任何想法吗?

CB *_*ley 6

模板参数std::copy不能在bind调用的上下文中推断出来.您需要明确指定它们:

copy< list<int>::iterator, ostream_iterator<int> >
Run Code Online (Sandbox Code Playgroud)

当你写:

for_each(a_list.begin().a_list.end(),
Run Code Online (Sandbox Code Playgroud)

我想你的意思是:

for_each(a_list.begin(),a_list.end(),
Run Code Online (Sandbox Code Playgroud)

你错过#include <iostream>了定义std::cout.