2011-06-20 16 views
2

我想創建一個擴展boost圖庫的行爲的類。我希望我的類是一個模板,用戶提供一個類型(類),用於存儲每個頂點的屬性。這只是背景。我正在努力創建一個更簡潔的typedef來定義我的新類。使用boost圖庫的模板化typedef湯

根據thisthis等其他帖子,我決定定義一個包含模板化typedefs的結構。

我將展示兩種密切相關的方法。我無法弄清楚爲什麼GraphType的第一個typedef似乎在工作,而第二個爲VertexType失敗。

#include <boost/graph/graph_traits.hpp> 
#include <boost/graph/adjacency_list.hpp> 

template <class VP> 
struct GraphTypes 
{ 
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP > GraphType; 
    typedef boost::graph_traits<GraphType>::vertex_descriptor VertexType; 
}; 

int main() 
{ 
    GraphTypes<int>::GraphType aGraphInstance; 
    GraphTypes<int>::VertexType aVertexInstance; 
    return 0; 
} 

編譯器輸出:

$ g++ -I/Developer/boost graph_typedef.cpp 
graph_typedef.cpp:8: error: type ‘boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP, boost::no_property, boost::no_property, boost::listS> >’ is not derived from type ‘GraphTypes<VP>’ 
graph_typedef.cpp:8: error: expected ‘;’ before ‘VertexType’ 
graph_typedef.cpp: In function ‘int main()’: 
graph_typedef.cpp:14: error: ‘VertexType’ is not a member of ‘GraphTypes<int>’ 
graph_typedef.cpp:14: error: expected `;' before ‘aVertexInstance’ 

同樣的事情,只是避免使用GraphType在第二的typedef:

#include <boost/graph/graph_traits.hpp> 
#include <boost/graph/adjacency_list.hpp> 

template <class VP> 
struct GraphTypes 
{ 
    typedef      boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP > GraphType; 
    typedef boost::graph_traits< boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP > >::vertex_descriptor VertexType; 
}; 

int main() 
{ 
    GraphTypes<int>::GraphType aGraphInstance; 
    GraphTypes<int>::VertexType aVertexInstance; 
    return 0; 
} 

編譯器輸出看起來實際上是相同的:

g++ -I/Developer/boost graph_typedef.cpp 
graph_typedef.cpp:8: error: type ‘boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP, boost::no_property, boost::no_property, boost::listS> >’ is not derived from type ‘GraphTypes<VP>’ 
graph_typedef.cpp:8: error: expected ‘;’ before ‘VertexType’ 
graph_typedef.cpp: In function ‘int main()’: 
graph_typedef.cpp:14: error: ‘VertexType’ is not a member of ‘GraphTypes<int>’ 
graph_typedef.cpp:14: error: expected `;' before ‘aVertexInstance’ 

顯然第一個編譯器錯誤是根本問題。我試圖在幾個地方插入typename,但沒有成功。我正在使用gcc 4.2.1

我該如何解決這個問題?

回答

5
typedef typename boost::graph_traits<GraphType>::vertex_descriptor VertexType; 
//  ^^^^^^^^ 

應該修復它,我不知道你在哪裏試圖把它雖然..你可能有其他的問題,我沒有看到。

+0

哦,我的天哪是對的。這將是我認爲拋出類型名稱令牌的最後一個地方。當然,typedef關鍵字後面的第一個參數應該是一個類型名稱。 – NoahR

+1

@Noah:確切地說,它必須位於依賴名稱(嵌套typedef或類/結構)之前。 – Xeo