TR1 在映射容器上绑定与成员函数(yikes!



我在以下函数方面遇到问题:

typedef std::tr1::shared_ptr<Island> Island_sp_t;
typedef map<string, Island_sp_t>::value_type island_map_pair;
std::map<std::string, Island_sp_t> Island_ptr_map;

void Model::save_all(ostream& out)
{
    for_each(Island_ptr_map.begin(), Island_ptr_map.end(), bind(&Island::save,
             bind<Island_sp_t>(&island_map_pair::second, _1), ref(out)));
}

我的Island::save函数如下所示

virtual void save(std::ostream& out) const;
到目前为止,这种

使用与地图绑定的格式对我有用,但仅在这种特殊情况下,我在 g++ 4.1.2 上收到以下错误,并带有以下标志:

g++ -c Pedantic -ansi -Wall

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h: In member function ‘typename std::tr1::result_of<_Functor ()(typename std::tr1::result_of<std::tr1::_Mu<_T1, std::tr1::is_bind_expression<_T1>::value, (std::tr1::is_placeholder<_T1>::value > 0)> ()(_T1, std::tr1::tuple<_U1, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass>)>::type, typename std::tr1::result_of<std::tr1::_Mu<_T1, std::tr1::is_bind_expression<_T1>::value, (std::tr1::is_placeholder<_T1>::value > 0)> ()(_T2, std::tr1::tuple<_U1, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass, std::tr1::_NullClass>)>::type)>::type std::tr1::_Bind<_Functor ()(_T1,
_T2)>::operator()(_U1&) [with _U1 = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, std::tr1::shared_ptr<Island> >, _Functor = std::tr1::_Mem_fn<void (Island::*)(std::ostream&)const>, _T1 = std::tr1::_Bind_result<std::tr1::shared_ptr<Island>, std::tr1::_Mem_fn<std::tr1::shared_ptr<Island> std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, std::tr1::shared_ptr<Island> >::*> ()(std::tr1::_Placeholder<1>)>, _T2 = std::tr1::reference_wrapper<std::basic_ostream<char, std::char_traits<char> > >]’: /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:159:   instantiated from ‘_Function std::for_each(_InputIterator,
_InputIterator, _Function) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::tr1::shared_ptr<Island> > >, _Function = std::tr1::_Bind<std::tr1::_Mem_fn<void (Island::*)(std::ostream&)const> ()(std::tr1::_Bind_result<std::tr1::shared_ptr<Island>, std::tr1::_Mem_fn<std::tr1::shared_ptr<Island> std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, std::tr1::shared_ptr<Island> >::*> ()(std::tr1::_Placeholder<1>)>, std::tr1::reference_wrapper<std::basic_ostream<char, std::char_traits<char> > >)>]’ Model.cpp:179:   instantiated from here /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h:45: error: no match for call to ‘(std::tr1::_Mem_fn<void (Island::*)(std::ostream&)const>) (std::tr1::shared_ptr<Island>, std::basic_ostream<char, std::char_traits<char> >&)’ /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:261: note: candidates are: _Res std::tr1::_Mem_fn<_Res (_Class::*)(_T1)const>::operator()(const _Class&, _T1) const [with
_Res = void, _Class = Island, _T1 = std::ostream&] /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:267: note:                 _Res std::tr1::_Mem_fn<_Res (_Class::*)(_T1)const>::operator()(const _Class*, _T1) const [with
_Res = void, _Class = Island, _T1 = std::ostream&] /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/functional_iterate.h:274: note:                 _Res std::tr1::_Mem_fn<_Res (_Class::*)(_T1)const>::operator()(_Tp&, _T1) const [with _Tp = std::tr1::shared_ptr<Island>, _Res = void, _Class = Island, _T1 = std::ostream&] /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/bind_iterate.h:45: error: return-statement with a value, in function returning 'void' make: *** [Model.o] Error 1

我对这个错误感到非常困惑,因为似乎所有类型都与functional_iterate.h:274候选者匹配良好。 关于我错过了什么的任何想法?

对于任何想知道的人,我需要在我的第二个绑定参数中使用"const",以便算法如下所示:

for_each(Island_ptr_map.begin(), Island_ptr_map.end(), bind(&Island::save,
             bind<const Island_sp_t>(&island_map_pair::second, _1), ref(out)));

相关内容

  • 没有找到相关文章

最新更新