1

以下无法编译对 t.get<1>() 的赋值。

struct pull_from_memory {
    pull_from_memory(MemoryBank &m) : m_map(m) {};
    void operator()(boost::tuple<string&, string&> &t ) {
        MemoryBank::iterator i;
        if ( ( i = m_map.find(t.get<0>())) != m_map.end() ) {
            t.get<1>() = *i;  // compilation error
        }
    }
private:
    MemoryBank &m_map;
};

错误消息 FWIW 是:

tom.cpp: In member function 'void pull_from_memory::operator()(boost::tuples::tuple<std::string&, std::string&, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>&)':
tom.cpp:424: error: no match for 'operator=' in '((boost::tuples::tuple<std::string&, std::string&, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>*)t)->boost::tuples::tuple<std::string&, std::string&, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>::<anonymous>.boost::tuples::cons<HT, TT>::get [with int N = 1, HT = std::string&, TT = boost::tuples::cons<std::string&, boost::tuples::null_type>]() = i.boost::unordered_detail::hash_iterator<A, G>::operator* [with A = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, G = boost::unordered_detail::ungrouped]()'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:510: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:518: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:529: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

更新 1:在纠正了 map.find 返回一对的事实之后,我现在得到一个链接错误,显然与实例化模板有关

/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h: In function '_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = boost::zip_iterator<boost::tuples::tuple<__gnu_cxx::__normal_iterator<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, __gnu_cxx::__normal_iterator<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> >, _Funct = bind_from_memory]':
/mxhome/charrison/git/sdcore/libs/CommUtils/test/../include/Block.h:160:   instantiated from 'void Block::traverse_record(const D_RecordType&, Function) [with Function = bind_from_memory]'
tom.cpp:473:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:4200: error: no match for call to '(bind_from_memory) (boost::tuples::cons<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::tuples::cons<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::tuples::null_type> >)'
tom.cpp:460: note: candidates are: void bind_from_memory::operator()(boost::tuples::tuple<const std::string&, std::string&, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>&)
4

2 回答 2

1

从外观上看,您尝试将 a 分配std::pair<std::string const, std::string>给 a std::string。这行不通。你的意思是写这个吗?

t.get<1>() = i->first;
于 2013-08-12T22:12:30.627 回答
1

您的问题是您期望*i成为 a std::string,但事实并非如此。这是一个(参考)std::pair<std::string,std::string>

这就是地图迭代器的工作方式。

试试这个:

t.get<1>() = i->second; //if you want the value corresponding to the key
于 2013-08-12T22:19:32.783 回答