使用boost图库的模板化typedef汤

Noa*_*ahR 2 c++ templates boost typedef

我正在尝试创建一个扩展boost图库行为的类.我希望我的类成为一个模板,用户提供一个类型(类),用于存储每个顶点的属性.那只是背景.我正在努力创建一个更简洁的typedef来用于定义我的新类.

基于其他职位喜欢这个这个,我决定来定义一个struct将包含模板的typedef.

我将展示两种密切相关的方法.我无法弄清楚为什么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;
}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

$ 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’
Run Code Online (Sandbox Code Playgroud)

同样的事情,只是避免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;
}
Run Code Online (Sandbox Code Playgroud)

编译器输出看起来有效:

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’
Run Code Online (Sandbox Code Playgroud)

显然第一个编译器错误是根本问题.我尝试typename在几个地方插入但没有成功.我正在使用gcc 4.2.1

我该如何解决?

Xeo*_*Xeo 5

typedef typename boost::graph_traits<GraphType>::vertex_descriptor VertexType;
//      ^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

应该修理它,我不知道你试图把它放在哪里..你可能还有其他问题,我看不到.