使用boost图库的模板typedef汤

Templated typedef soup using boost graph library

本文关键字:typedef boost 使用      更新时间:2023-10-16

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

基于其他类似的文章,我决定定义一个包含模板化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’

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

#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

我该如何解决这个问题?

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

应该把它修好,不过我不知道你想把它放在哪里。。你可能还有其他问题,我看不出来。