【问题标题】:C++ boost::bimap insert() method not workingC++ boost::bimap insert() 方法不起作用
【发布时间】:2019-11-13 21:41:18
【问题描述】:

我正在尝试在 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

【问题讨论】:

  • 你能说明nameobs_id 指的是什么吗?我无法重现该问题...
  • 欢迎来到 Stack Overflow。请阅读the help pagesthe SO tour、阅读how to ask good questions,以及this question checklist。最后请学习如何创建minimal reproducible example
  • @lubgr :该值来自诸如 (std::string & name, const std::string & obs_id) 之类的方法调用,字段内的值类似于 name = "Domain_01" 和 obs_id =“ae23b458”
  • 我猜你的成员函数(_obs_ids.insert 被调用的地方)是const,不是吗?
  • 我已经添加了完整的功能。请检查。

标签: c++ c++11 boost bimap


【解决方案1】:

您必须从成员函数签名中删除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 实例,所以只能从非常量函数中调用该函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多