以下内容在分配给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>]
UPDATE 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>&)
您的问题是您希望*i
是std::string
,但事实并非如此。这是对std::pair<std::string,std::string>
的引用。
这就是映射迭代器的工作方式。
试试这个:
t.get<1>() = i->second; //if you want the value corresponding to the key
从外观上看,您尝试将std::pair<std::string const, std::string>
分配给std::string
。这行不通。你是有意写这个的吗?
t.get<1>() = i->first;