如何为使用自定义分配器的字符串重载赋值运算符

时间:2014-10-19 18:05:17

标签: c++ boost

我正在尝试在共享内存中使用boost MultiIndex容器,我使用replace函数来更新记录,在尝试编译时,编译器抱怨operator =不存在所以我重载了operator =如图所示在下面的代码中,但似乎抛出的编译错误是由于分配器。这是我第一次使用自定义分配器来共享内存。在使用自定义分配器重载operator = for strings时,我们需要做些什么特别的事情吗?

  1 #include <boost/interprocess/managed_shared_memory.hpp>
  2 #include <boost/interprocess/allocators/allocator.hpp>
  3 #include <boost/interprocess/containers/string.hpp>
  4
  5 #include <boost/multi_index_container.hpp>
  6 #include <boost/multi_index/member.hpp>
  7 #include <boost/multi_index/ordered_index.hpp>
  8 #include <iostream>
  9
 10 using namespace boost::interprocess;
 11 namespace bmi = boost::multi_index;
 12
 13 typedef managed_shared_memory::allocator<char>::type              char_allocator;
 14 typedef basic_string<char, std::char_traits<char>, char_allocator> shm_string;
 15
 16 //Data to insert in shared memory
 17 struct tickerUpdateInfo
 18 {
 19   shm_string  id;
 20   shm_string  symbol;
 21   int         last_update_time;
 22
 23   tickerUpdateInfo( const char * id_,
 24       const char *symbol_,
 25       int last_update_time_,
26       const char_allocator &a)
 27     : id(id_, a), symbol(symbol_, a), last_update_time(last_update_time_) {
 28     }
 29
 30   tickerUpdateInfo& operator=(const tickerUpdateInfo& other)
 31   {
 32     if (this != &other) {
 33       id = other.id;
 34       symbol = other.symbol;
 35       last_update_time = other.last_update_time;
 36     }
 37     return *this;
 38   }
 39 };
 40
 41 std::ostream& operator<<(std::ostream& os, const tickerUpdateInfo& obj)
 42 {
 43   // write obj to stream
 44   os << obj.id << " ";
 45   os << obj.symbol << " ";
 46   os << obj.last_update_time << " " << std::endl;
 47   return os;
 48 };
 49
 50
 51 //Tags
 52 struct id{};
 53 struct symbol{};
 54 struct last_update_time{};
 55
 56 // Define a multi_index_container of tickerUpdateInfos with following indices:
 57 //   - a unique index sorted by tickerUpdateInfo::id,
 58 //   - a unique index sorted by tickerUpdateInfo::symbol,
 59 //   - a non-unique index sorted by tickerUpdateInfo::last_update_time.
 60 typedef bmi::multi_index_container<
 61 tickerUpdateInfo,
 62   bmi::indexed_by<
 63   bmi::ordered_unique
 64   <bmi::tag<id>,  BOOST_MULTI_INDEX_MEMBER( tickerUpdateInfo, shm_string, id)>,
 65   bmi::ordered_unique<
 66   bmi::tag<symbol>,BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, shm_string, symbol)>,
 67   bmi::ordered_non_unique
 68   <bmi::tag<last_update_time>, BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, int, last_update_time)> >,
 69   managed_shared_memory::allocator<tickerUpdateInfo>::type
 70   > tickerUpdateInfo_set;
 71
 72 int main ()
 73 {
 74   //Remove shared memory on construction and destruction
 75   struct shm_remove
 76   {
 77     shm_remove() { shared_memory_object::remove("MySharedMemory"); }
 78     ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
 79   } remover;
 80
 81   //Create shared memory
 82   managed_shared_memory segment(create_only,"MySharedMemory", 65536);
 83
 84   //Construct the multi_index in shared memory
 85   tickerUpdateInfo_set *es = segment.construct<tickerUpdateInfo_set>
 86     ("TickerUpdateContainer")            //Container's name in shared memory
 87     ( tickerUpdateInfo_set::ctor_args_list()
 88       , segment.get_allocator<tickerUpdateInfo>());  //Ctor parameters
 89
 90   //Now insert elements
 91   char_allocator ca(segment.get_allocator<char>());
 92   es->insert(tickerUpdateInfo("0","Joe", 31,ca));
 93   es->insert(tickerUpdateInfo("1", "Robert",27, ca));
 94   es->insert(tickerUpdateInfo( "2","John", 36,ca));
 95   const tickerUpdateInfo_set::nth_index<1>::type& name_index = (*es).get<1>();
 96   std::cout << "Before update " << std::endl;
 97   std::copy(
 98       name_index.begin(),name_index.end(),
 99       std::ostream_iterator<tickerUpdateInfo>(std::cout));
100
101
102   typedef tickerUpdateInfo_set::index<symbol>::type ticker_update_info_set_by_symbol;
103   ticker_update_info_set_by_symbol & nm_index = (*es).get<symbol>();
104   ticker_update_info_set_by_symbol::iterator it=nm_index.find("Joe");
105   tickerUpdateInfo ticker_info = *it;
106   ticker_info.symbol = "Deb";      // update key
107   nm_index.replace(it, ticker_info ); // update her record
108   std::cout << "After update " << std::endl;
109   std::copy(
110       nm_index.begin(),nm_index.end(),
111       std::ostream_iterator<tickerUpdateInfo>(std::cout));
112   return 0;
113 }

编译错误:

   -- Compiling src/writer.cxx
In file included from include/boost/multi_index/ordered_index.hpp:56,
                 from src/writer.cxx:7:
include/boost/multi_index/detail/ord_index_ops.hpp: In constructor 'boost::container::basic_string<CharT, Traits, Alloc>::basic_string(const CharT*, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]':
include/boost/multi_index/detail/ord_index_ops.hpp:67:   instantiated from 'Node* boost::multi_index::detail::ordered_index_find(Node*, Node*, const KeyFromValue&, const CompatibleKey&, const CompatibleCompare&) [with Node = boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<tickerUpdateInfo, boost::interprocess::allocator<tickerUpdateInfo, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > > >, KeyFromValue = boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, CompatibleKey = char [4], CompatibleCompare = std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >]'
include/boost/multi_index/ordered_index.hpp:434:   instantiated from 'boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<typename SuperMeta::type::node_type> > boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::find(const CompatibleKey&) const [with CompatibleKey = char [4], KeyFromValue = boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, Compare = std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >, SuperMeta = boost::multi_index::detail::nth_layer<2, tickerUpdateInfo, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<id, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::id>, mpl_::na>, boost::multi_index::ordered_unique<boost::multi_index::tag<symbol, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, mpl_::na>, boost::multi_index::ordered_non_unique<boost::multi_index::tag<last_update_time, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, int, &tickerUpdateInfo::last_update_time>, mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::interprocess::allocator<tickerUpdateInfo, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, TagList = boost::mpl::v_item<symbol, boost::mpl::vector0<mpl_::na>, 0>, Category = boost::multi_index::detail::ordered_unique_tag]'
src/writer.cxx:107:   instantiated from here
include/boost/multi_index/detail/ord_index_ops.hpp:67: error:  no matching function for call to 'boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >::allocator()'
include/boost/interprocess/allocators/allocator.hpp:130: note: candidates are: boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T, SegmentManager>&) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>]
include/boost/interprocess/allocators/allocator.hpp:125: note: boost::interprocess::allocator<T, SegmentManager>::allocator(SegmentManager*) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>]
include/boost/multi_index/detail/ord_index_ops.hpp:74:   instantiated from 'Node* boost::multi_index::detail::ordered_index_find(Node*, Node*, const KeyFromValue&, const CompatibleKey&, const CompatibleCompare&) [with Node = boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<tickerUpdateInfo, boost::interprocess::allocator<tickerUpdateInfo, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > > >, KeyFromValue = boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, CompatibleKey = char [4], CompatibleCompare = std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >]'
include/boost/multi_index/ordered_index.hpp:434:   instantiated from 'boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<typename SuperMeta::type::node_type> > boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::find(const CompatibleKey&) const [with CompatibleKey = char [4], KeyFromValue = boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, Compare = std::less<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > > >, SuperMeta = boost::multi_index::detail::nth_layer<2, tickerUpdateInfo, boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<id, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::id>, mpl_::na>, boost::multi_index::ordered_unique<boost::multi_index::tag<symbol, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, &tickerUpdateInfo::symbol>, mpl_::na>, boost::multi_index::ordered_non_unique<boost::multi_index::tag<last_update_time, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::multi_index::member<tickerUpdateInfo, int, &tickerUpdateInfo::last_update_time>, mpl_::na>, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::interprocess::allocator<tickerUpdateInfo, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, TagList = boost::mpl::v_item<symbol, boost::mpl::vector0<mpl_::na>, 0>, Category = boost::multi_index::detail::ordered_unique_tag]'
src/writer.cxx:107:   instantiated from here
include/boost/multi_index/detail/ord_index_ops.hpp:74: error:  no matching function for call to 'boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >::allocator()'
include/boost/interprocess/allocators/allocator.hpp:130: note: candidates are: boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T, SegmentManager>&) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>]
include/boost/interprocess/allocators/allocator.hpp:125: note:                 boost::interprocess::allocator<T, SegmentManager>::allocator(SegmentManager*) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>]

更新

我已经改变了代码的结构并使用了你所建议的仿函数,我仍然看到问题,我正在粘贴下面的代码片段: //TickerInfoMangerImplementation.cxx

      1 #include <TickerInfoMangerImplementation.h>
      2 #include <boost/interprocess/managed_shared_memory.hpp>
      3 #include <iostream>
      4
      5 using namespace boost::interprocess;
      6
      7 tickerInfoMangerImplementation::tickerInfoMangerImplementation( const sharedMemoryNameT & name ): m_name(name),
      8   m_managed_memory_segment( create_only, "test", 65536 )
      9 {
     10
     11   p_ticker_info_set = m_managed_memory_segment.construct<ticker_update_info_set>
     12     ("SetOfTickerUpdateInformation")            //Container's name in shared memory
     13     ( ticker_update_info_set::ctor_args_list()
     14       , m_managed_memory_segment.get_allocator<tickerUpdateInfoT>());  //Ctor parameters
     15 }
     16
     17 bool tickerInfoMangerImplementation::put_records( const tickerUpdateInfoT & record ) {
     18
     19   std::pair<ticker_update_info_set::iterator, bool> result_pair = p_ticker_info_set->insert( record );
     20   if( result_pair.second ) {
     21     return result_pair.second;
     22   }
     23
     24   typedef ticker_update_info_set::index<symbol_index>::type ticker_update_info_set_by_symbol;
     25   ticker_update_info_set_by_symbol & sym_index = (*p_ticker_info_set).get<symbol_index>();
     26   ticker_update_info_set_by_symbol::iterator it = sym_index.find( record.m_symbol );
     27   tickerUpdateInfoT ticker_info = *it;
     28   ticker_info.m_last_update_time = record.m_last_update_time;
     29   return sym_index.replace( it, ticker_info );
     30 }
     31
     32 int tickerInfoMangerImplementation::get_active_ticker_count( const thresholdT seconds ) {
     33 }
     34
     35 void tickerInfoMangerImplementation::print_contents() {
     36   const ticker_update_info_set::nth_index<1>::type& name_index = (*p_ticker_info_set).get<1>();
     37   std::copy( name_index.begin(), name_index.end(), std::ostream_iterator<tickerUpdateInfoT>(std::cout) );
     38 }
     39
     40 std::ostream& operator<<(std::ostream& os, const tickerUpdateInfoT & obj) {
     41   os << obj.m_id << " ";
     42   os << obj.m_symbol << " ";
     43   os << obj.m_last_update_time << " " << "\n";
     44   return os;
     45 };

//TickerInfoMangerImplementation.h

      1 #ifndef __TICKER_INFO_MANAGER_IMPL__
  2 #define __TICKER_INFO_MANAGER_IMPL__
  3
  4 #include <boost/interprocess/containers/string.hpp>
  5 #include <boost/interprocess/shared_memory_object.hpp>
  6 #include <boost/multi_index_container.hpp>
  7 #include <boost/multi_index/member.hpp>
  8 #include <boost/multi_index/ordered_index.hpp>
  9 #include <TickerInfoManagerConstants.h>
 10 #include <TickerInfo.h>
 11
 12 namespace bmi = boost::multi_index;
 13 namespace bip = boost::interprocess;
 14
 15 struct id_index{};
 16 struct symbol_index{};
 17 struct last_update_time_index{};
 18
 19 struct Less {
 20   template<class T, class U>
 21     bool operator()(T const& t, U const& u) const {
 22       return t < u;
 23     }
 24 };
 25
 25
 26
 27 typedef bmi::multi_index_container<
 28 tickerUpdateInfoT,
 29   bmi::indexed_by<
 30   bmi::ordered_unique
 31   <bmi::tag<id_index>,  BOOST_MULTI_INDEX_MEMBER( tickerUpdateInfo, shm_string, m_id), Less>,
 32   bmi::ordered_unique<
 33   bmi::tag<symbol_index>,BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, shm_string, m_symbol), Less>,
 34   bmi::ordered_non_unique
 35   <bmi::tag<last_update_time_index>, BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, int, m_last_update_time), Less> >,
 36   bip::managed_shared_memory::allocator<tickerUpdateInfo>::type
 37   > ticker_update_info_set;
 38
 39   class tickerInfoMangerImplementation {
 40
 41     public:
 42       tickerInfoMangerImplementation( const sharedMemoryNameT & name );
 43
 44       bool put_records( const tickerUpdateInfoT & record );
 45
 46       int get_active_ticker_count( const thresholdT seconds );
 47
 48       void print_contents();
 49
 50       bip::managed_shared_memory& get_managed_memory_segment() {
 51         return m_managed_memory_segment;
 52       }
 53
 54     private:
 55       const sharedMemoryNameT    m_name;
 56       bip::managed_shared_memory m_managed_memory_segment;
 57       ticker_update_info_set     *p_ticker_info_set;
 58   };
 59 #endif

// TickerInfo.h

  1 #ifndef __TICKER_INFO__
  2 #define __TICKER_INFO__
  3
  4 #include <boost/interprocess/managed_shared_memory.hpp>
  5 #include <boost/interprocess/allocators/allocator.hpp>
  6 #include <boost/interprocess/containers/string.hpp>
  7
  8 typedef boost::interprocess::managed_shared_memory::allocator<char>::type               char_allocator;
  9 typedef boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator> shm_string;
 10
 11 //Data to insert in shared memory
 12 typedef struct tickerUpdateInfo{
 13
 14   shm_string  m_id;
 15   shm_string  m_symbol;
 16   int         m_last_update_time;
 17
 18   tickerUpdateInfo( const char * id,
 19       const char *symbol,
 20       int last_update_time,
 21       const char_allocator &a)
 22     : m_id( id, a), m_symbol( symbol, a), m_last_update_time( last_update_time) {
 23     }
 24 } tickerUpdateInfoT;
 25

错误日志:

tor0,0&gt;,Category = boost :: multi_index :: detail :: ordered_unique_tag]&#39; /home/user/droy/src/quotes/debshmutils/shmdb/src/TickerInfoMangerImplementation.cxx:29:从这里实例化 /home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/multi_index/detail/index_base.hpp:114:错误:不匹配&#39;运营商=&#39; in&#39; x-&gt; boost :: multi_index :: detail :: index_node_base :: value with Value = tickerUpdateInfo,Allocator = boost :: interprocess :: allocator,0ul&gt;,boost :: interprocess :: iset_index&gt; &GT; = v&#39; include / TickerInfo.h:12:注意:候选人是:tickerUpdateInfo&amp; tickerUpdateInfo ::运算=(tickerUpdateInfo&安培;)

请关注我,因为我不清楚这里发生了什么。谢谢!

1 个答案:

答案 0 :(得分:1)

问题是ordered_unique / ordered_non_unique索引默认使用std::less<K>。当您使用兼容密钥nm_index.find("Joe")char const[]&#34; Joe&#34;进行搜索时需要在shm_string中转换为std::less<K>::operator()(shm_string const&, shm_string const&),但shm_string构造函数还需要一个allocator参数。

解决方案是提供自己的不转换参数的比较类:

struct Less
{
    template<class T, class U>
    bool operator()(T const& t, U const& u) const {
        return t < u;
    }
};

// Define a multi_index_container of tickerUpdateInfos with following indices:
//   - a unique index sorted by tickerUpdateInfo::id,
//   - a unique index sorted by tickerUpdateInfo::symbol,
//   - a non-unique index sorted by tickerUpdateInfo::last_update_time.
typedef bmi::multi_index_container<
    tickerUpdateInfo,
    bmi::indexed_by<
          bmi::ordered_unique<bmi::tag<id>, BOOST_MULTI_INDEX_MEMBER( tickerUpdateInfo, shm_string, id), Less>,
          bmi::ordered_unique<bmi::tag<symbol>,BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, shm_string, symbol), Less>,
          bmi::ordered_non_unique<bmi::tag<last_update_time>, BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, int, last_update_time), Less>
          >,
    managed_shared_memory::allocator<tickerUpdateInfo>::type
> tickerUpdateInfo_set;

tickerUpdateInfo& operator=(const tickerUpdateInfo& other)不是必需的,编译器生成的一个做同样的事情。