c++ - boost::interprocess::ptree 的前向声明

标签 c++ forward-declaration boost-propertytree

我想使用 boost::property_treeptree 类的前向声明。

我使用 Visual Studio 2010 和 boost 版本 1.48.0。

我在我的 .h 中按以下方式进行前向声明

#ifndef OPTIONS_H_
#define OPTIONS_H_

namespace boost
{
    namespace property_tree
    {
        class ptree;
    }
}

class Options
{
   // something

private:
    boost::property_tree::ptree *m_pxPropertyTree;
};

#endif // OPTIONS_H_

然后,我在 .cpp 中使用该类

#include <boost/property_tree/ptree.hpp>

using boost::property_tree::ptree;

Options::Options()
{
    m_pxPropertyTree = new ptree();

    // other stuff
}

当我尝试编译它时,出现以下错误

error C2371: 'boost::property_tree::ptree': redefinition. Different base type. c:\lib\boost\1.48.0\32\boost\property_tree\ptree_fwd.hpp 95

(错误描述可能有所不同,我已经翻译了它,因为我有意大利语版本的 Visual Studio)。

ptree_fwd.hpp 中给我错误的行如下

typedef basic_ptree<std::string, std::string> ptree;

相反,如果我不使用前向声明,一切都会顺利并且编译成功。

我做错了什么以及在这种情况下如何正确使用前向声明?

最佳答案

为什么不直接包含 boost/property_tree/ptree_fwd.hpp?此 header 包含包的所有前向声明。

编辑:没有包含的解决方案(您想避免使用 充分的理由)是与实际声明的内容完全匹配。

所以:

#include <string>
#include <functional>
namespace boost
{
  namespace property_tree
  {
    template < class Key, class Data, class KeyCompare >
    class basic_ptree;

    typedef basic_ptree< std::string, std::string, std::less<std::string> > ptree;
  }
}

关于c++ - boost::interprocess::ptree 的前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9433398/

相关文章:

python - Django 模型 : mutual references between two classes and impossibility to use forward declaration in python

c++ - Boost read_json 和 write_json 对属性树不等价

c++ - 具有通用获取/设置实现的类属性树数据结构的框架/库?

c++ - boost Boost 属性树性能

c++ - 奇怪的段错误案例

c++ - Catch2:测试崩溃,因为封装在REQUIRE_THROWS中的调用引发异常

c++ - 我收到 [Error] 'Eintrag' does not name a type 我不知道为什么

c++ - 通用常数时间比较函数 C++

c# - 通过 C++ dll 或 C++ exe 编写 C# GUI

c++ - 当类包含在另一个类中时,为什么不转发类工作的声明