C++ boost::进程间::ptree的前向声明
我想对C++ boost::进程间::ptree的前向声明,c++,forward-declaration,boost-propertytree,C++,Forward Declaration,Boost Propertytree,我想对boost::property_tree的ptree类使用转发声明 我使用Visual Studio 2010和boost 1.48.0版 我在我的h.h.中按以下方式进行远期申报 #ifndef OPTIONS_H_ #define OPTIONS_H_ namespace boost { namespace property_tree { class ptree; } } class Options { // something pr
boost::property_tree
的ptree
类使用转发声明
我使用Visual Studio 2010和boost 1.48.0版
我在我的h.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
}
#包括
使用boost::property_tree::ptree;
选项::选项()
{
m_pxPropertyTree=新的ptree();
//其他东西
}
当我试图编译它时,我得到以下错误
错误C2371:“boost::property_tree::ptree”:重新定义。不同的基本类型。c:\lib\boost\1.48.0\32\boost\property\u tree\ptree\u fwd.hpp 95
(错误描述可能不同,我翻译了它,因为我有Visual Studio的意大利版本)
在ptree_fwd.hpp中,给出错误的行如下
typedef basic_ptree<std::string, std::string> ptree;
typedef基本目录树;
相反,如果我不使用forward声明,那么一切都会顺利进行,并且我成功地编译了它
我做错了什么,以及在这种情况下如何正确使用转发声明?为什么不包括
boost/property\u tree/ptree\u fwd.hpp
?此标头包含包的所有转发声明
编辑:未包含的解决方案(您希望避免使用该解决方案)
很好的理由)是完全匹配实际声明的内容
因此:
#包括
#包括
名称空间提升
{
命名空间属性树
{
模板<类键、类数据、类键比较>
类基本树;
typedef basic_ptreeptree;
}
}
ptree
不是一个类,而是一个typedef
(即另一种类型的别名)。这意味着当您尝试将其作为类进行前向声明时,类和typedef将发生冲突。如果您不必在头中包含头,最好不要包含头。因为我的编译速度更快,如果其他人必须使用我的库,我不需要包含那些boost头。@LuchianGrigore True。它看起来也像是头在拉一些不需要的东西,比如可选的\u 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;
}
}