2016-09-22 117 views
1

我正在研究使用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 

這裏的錯誤,而鏗鏘構建代碼是

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")); 

但是當我註釋掉向前聲明該編譯完全正常並且只包含啊中的Attributes頭當我使用g ++代替clang ++時,代碼也在構建中。

我不明白一些事情 -

1)爲什麼不鏗鏘++當有正向的聲明,包括建築?

2)爲什麼錯誤指向obixercesc_2_8 :: Attributes,而不是XERCES_CPP_NAMESPACE :: Attributes,類Attributes的實際名稱空間?

3)爲什麼代碼用g ++編譯?

回答

1

這是一個比解決方案更多的假設,但無論如何,這裏有一個想法。

出於某種原因,你是前瞻性聲明Attributes錯誤地嵌套命名空間obixercesc_2_8::obixercesc_2_8,當你指的是obixercesc_2_8::Attributes,鐺選擇您的向前聲明瞭從Xerces的實施,因爲它們不在同一個命名空間(也許因爲一個using namespace聲明?)。從它的觀點來看,你有兩個聲明Attributes,一個在obixercesc_2_8和一個在obixercesc_2_8::obixercesc_2_8XERCES_CPP_NAMESPACE似乎是一個擴展到obixercesc_2_8的宏。

+0

這是問題所在。謝謝。 – Sashank