【问题标题】:Error with both include and forward declaration包含和前向声明都出错
【发布时间】:2016-09-22 06:31:04
【问题描述】:

我正在开发一个使用 Apache Xerces 的庞大代码库。我正在使用 clang++ 构建代码,它给出了一个错误。

在一个特定的.h文件a.ha.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


    【解决方案1】:

    这更像是一个假设而不是一个解决方案,但无论如何,这是一个想法。

    由于某种原因,您在嵌套命名空间 obixercesc_2_8::obixercesc_2_8 中错误地前向声明 Attributes,当您引用 obixercesc_2_8::Attributes 时,CLang 选择前向声明而不是 Xerces 的实现,因为它们不在同一个命名空间中(也许是因为using namespace 声明?)。从它的角度来看,您有两个Attributes 声明,一个在obixercesc_2_8 中,一个在obixercesc_2_8::obixercesc_2_8 中。 XERCES_CPP_NAMESPACE 似乎是一个扩展为 obixercesc_2_8 的宏。

    【讨论】:

    • 这就是问题所在。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多