我想对ptree类boost::property_tree使用前向声明。
我使用Visual 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。不同的基本类型。c:\lib\boost\1.48.0\32\boost\property_tree\ptree_fwd.hpp 95
(错误描述可能是不同的,我翻译它是因为我有意大利版本的Visual )。
在ptree_fwd.hpp中,给出错误的行如下所示
typedef basic_ptree<std::string, std::string> ptree;相反,如果我不使用前向声明,一切都进行得很顺利,我成功地编译了它。
在这种情况下,我做错了什么,如何正确地使用前向声明?
发布于 2012-02-24 15:55:03
你为什么不直接加入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;
}
}https://stackoverflow.com/questions/9433398
复制相似问题