Boost :: tuple — назначение для получения & lt; & gt; ()

Следующее не в состоянии скомпилировать при назначении 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>&)

1

Решение

Ваша проблема в том, что вы ожидаете *i быть std::string, но это не так. Это (ссылка на) std::pair<std::string,std::string>,

Так работают итераторы для карт.

Попробуйте это вместо этого:

t.get<1>() = i->second; //if you want the value corresponding to the key
1

Другие решения

Судя по всему, вы пытаетесь назначить std::pair<std::string const, std::string> к std::string, Это не сработает. Ты хотел написать это?

t.get<1>() = i->first;
1

По вопросам рекламы [email protected]