首页 > 解决方案 > 尝试访问 dynamic_bitset 中的私有成员

问题描述

我正在使用 Visual Studio 2017 构建 LucenePlusPlus,而后者又使用 boost/dynamic_bitset。

LucenePlusPlus 中的以下代码

const uint64_t* BitSet::getBits() {
    return bitSet.empty() ? NULL : static_cast<const uint64_t*>(&bitSet.m_bits[0]);
}

生成以下致命编译器错误:

1>g:\luceneplusplus\src\core\util\bitset.cpp(20): error C2248: 'boost::dynamic_bitset<uint64_t,std::allocator<Block>>::m_bits': cannot access private member declared in class 'boost::dynamic_bitset<uint64_t,std::allocator<Block>>'
1>        with
1>        [
1>            Block=uint64_t
1>        ]

建议?

标签: boost

解决方案


我已经跟踪了我的 boost 1_75_0 的代码,我看到它有一个定义,可以有条件地保留对朋友的访问权限:

#if defined(BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS)
#define BOOST_DYNAMIC_BITSET_PRIVATE public
#else
#define BOOST_DYNAMIC_BITSET_PRIVATE private
#endif

所以看起来你应该能够添加-DBOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS到编译器定义来解决这个问题。

事实上,我的 LucenePlusPlus 树已经包含在 include/config_h/Config.h.in 第 107 行中:

 #define BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS

以前(“重建的 cmake 构建系统”)这显然来自 include/Config.h.cmake,它始终存在,自 2010 年以来一直无条件地存在: 在此处输入图像描述

你能做什么?

也许您没有包含包含配置的必需标头?还要检查任何干扰的预编译头设置。

如果您boost/dynamic_bitset.hpp在任何时候都包含了 LucenePlusPlus 标头,那么您将使用错误的配置运行。

如果您没有检测到它,这尤其糟糕,因为这会违反 ODR 规则


推荐阅读