【发布时间】:2016-09-22 06:31:04
【问题描述】:
我正在开发一个使用 Apache Xerces 的庞大代码库。我正在使用 clang++ 构建代码,它给出了一个错误。
在一个特定的.h文件a.h,a.cpp的头文件中,类属性的头文件的前向声明和包含如下 -
#include <xercesc/sax2/Attributes.hpp>
和
namespace XERCES_CPP_NAMESPACE{
class Attributes;
}
文件xercesc/sax2/Attributes.hpp有代码
XERCES_CPP_NAMESPACE_BEGIN
...<some code>...
class SAX2_EXPORT Attributes {
...<some code>...
}
...<some code>...
XERCES_CPP_NAMESPACE_END
使用 clang 构建代码时的错误是
a.cpp:45:39: error: member access into incomplete type 'const obixercesc_2_8::Attributes'
a.h:20:10: forward declaration of 'obixercesc_2_8::obixercesc_2_8::Attributes'
class Attributes;
这是 a.cpp 中引发错误的相应行
void f(const XERCES_CPP_NAMESPACE::Attributes& attrs) {
/* this line ---> */ const XMLCh * pAppName = attrs.getValue(X("appName"));
但是当我注释掉前向声明并仅在 a.h 中包含 Attributes 标头时,这编译得非常好。当我使用 g++ 而不是 clang++ 时,代码也在构建中。
我不明白一些事情 -
1) 当同时存在前向声明和包含时,为什么不使用 clang++ 构建?
2) 为什么错误指向 obixercesc_2_8::Attributes,而不是 XERCES_CPP_NAMESPACE::Attributes,即类 Attributes 的实际命名空间?
3) 为什么代码是用g++编译的?
【问题讨论】:
标签: c++ include forward-declaration clang++ xerces