C boost :: bimap insert()方法不起作用

C++ boost::bimap insert() method not working

本文关键字:方法 不起作用 insert boost bimap      更新时间:2023-10-16

我试图将数据插入boost :: bimap,但给出了一个错误。

typedef boost::bimap<std::string, std::string> bimap_type;
bimap_type _obs_ids;
void store_domains_obs_ids(const std::string & name, const std::string & obs_id) const
{
    _obs_ids.insert(bimap_type::value_type(name, obs_id));
}

错误::

no instance of overloaded function "boost:thimaps::bimap<KeyTypeA, KeyTypeB, AP1, AP2, AP3>::insert [with KeyTypeA= std::string, KeyTypelhstd::string, AP1=boost::mpl::na, AP2= boost::mpl::na, AP3= boost:mph:na]" matches the argument list and object (the object has type qualifiers that prevent a match) argument types are: (boostthimapsuelation::mutant_relation<boostthimaps::tags::tagged<const std::string, boost:thimapsuelation::member_athleft>, boost::bimaps::tags::tagged<const std::string, boostthimapsuelation::member_athright>, boost::mpl::na, false>) object type is: const ipm::config::bimap_type

您必须从成员函数签名中删除 const

void store_domains_obs_ids(const std::string & name, const 
std::string & obs_id) const
{}

应该是

void store_domains_obs_ids(const std::string & name, const 
std::string & obs_id) 
{}

内部 const 成员函数, _obs_ids被视为const对象,对于const对象,您只能调用 const 函数成员。因为insert修改了bimap实例,因此您只能从非const函数调用此函数。