如何解决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 举报,一经查实,本站将立刻删除。