微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

boost :: multiindex和继承

如何解决boost :: multiindex和继承

我正在尝试从boot :: multiindex继承,并查看我能做些什么,与此同时,insert可以正常工作,而replace则不能。

代码

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <string>
#include <utility>
#include <iostream>

using namespace boost::multi_index;

struct employee
{
  std::string name_;
  int id;
  int state;

  employee(std::string name,int id) 
      : name_{std::move(name)},id(id),state(0) {}


  employee(const employee& copy):
      name_(copy.name_),id(copy.id),state(copy.state){}
       
  bool operator<(const employee &a) const 
  { 
      return id < a.id; 
  }

  const std::string& get_name() const 
  { 
      return name_; 
  }

};

struct names{};

typedef multi_index_container<
  employee,indexed_by<
    ordered_unique<
      identity<employee>
    >,hashed_unique<
      tag<names>,const_mem_fun<
        employee,const std::string&,&employee::get_name
      >
    >
  >
> employee_container;


typedef employee_container::index<names>::type::iterator employee_iterator_by_name;
//using employee_by_name = employee_container::nth_index<ANTENNA_INDEX_BY_NAME>::type&;

class employee_db: public employee_container
{
    public:
      void add_db(const employee& e)
      {
         this->insert(e);
      }  
      void update_db(
             employee_iterator_by_name& it
                )
      {
         this->replace(it,*it);
      }  


};  

在Linux中编译

gcc version 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC) 

[sandBox@localhost multiindex]$ rpm -qa | grep boost | grep devel
boost-devel-1.66.0-7.el8.x86_64

[sandBox@localhost multiindex]$ g++  2.cc -c -std=c++11

2.cc: In member function ‘void employee_db::update_db(employee_iterator_by_name&)’:
2.cc:75:31: error: no matching function for call to ‘employee_db::replace(employee_iterator_by_name&,const value_type&)’
          this->replace(it,*it);

我做错了什么?

解决方法

update_db中,您使用employee_iterator_by_name,它是replace的索引#0版本(默认索引)的索引#1的迭代器。您要使用索引#1 replace

  void update_db(employee_iterator_by_name& it)
  {
     this->get<names>().replace(it,*it);
  }  

我理解这只是一些探索性代码,因为以这种方式调用replace不会改变任何内容(您将it指向的值替换为{ {1}}。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?