2

我想ptreeboost::property_tree.

我使用 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
}

当我尝试编译它时,我收到以下错误

错误 C2371:'boost::property_tree::ptree':重新定义。不同的基础类型。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;

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

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

4

1 回答 1

3

为什么不包括在内boost/property_tree/ptree_fwd.hpp?此标头包含包的所有前向声明。

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

所以:

#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;
  }
}
于 2012-02-24T15:55:03.713 回答