2012-04-15 59 views
0

我遇到類似this question一個問題,但有一個額外的深度:指的是表弟的命名空間

namespace root { namespace parent1 { namespace childa { 
    class hard_to_get_at{}; 
}}} 

namespace root { namespace parent2 { namespace childb { 
    // how do I refer refer to namespace childb relative to the current namespace ? 
    void someFunc() 
    { 
     parent1::childa::hard_to_get_at instance; // does not work 
    } 
}}} 

當我試圖上面,我得到一個錯誤

錯誤:「根: :parent2 :: childb :: parent1 :: childa'還沒有宣佈

我不明白爲什麼這不起作用,我得到它應該的印象。我真的不想在someFunc函數中使用使用聲明。

這是發生在G ++ 4.5用C++ 0x中選擇啓用

+0

您可能在代碼中缺少包含,或者在問題中存在大量重要細節。如果包含是正確的,那麼你可能有多個名稱空間/類具有相同的名稱,編譯器選擇了不正確的名稱空間。 [檢查此](http://ideone.com/bsy8q) – 2012-04-15 20:21:21

+0

您發佈的代碼爲我編譯罰款。 – 2012-04-15 20:22:29

回答

2

你錯過了一些開放括號:

namespace root { namespace parent1 { namespace childa { // <--- here 
    class hard_to_get_at{}; 
}}} 

namespace root { namespace parent2 { namespace childb { // <--- and here 
    // how do I refer refer to namespace childb relative to the current namespace ? 
    void someFunc() 
    { 
     parent1::childa::hard_to_get_at instance; // does not work 
    } 
}}} 

這其中的道理縮進是非常重要的一個。

+0

對不起,這不是故意的,我解決了這個問題 – lurscher 2012-04-15 20:16:47

+1

@lurscher那麼,它應該編譯:http://ideone.com/gFBSB – 2012-04-15 20:17:52

+0

@lurscher你應該複製粘貼代碼,它可能是一個不同的問題。 – 2012-04-15 20:19:53