开发者

Templated typedef soup using boost graph library

I am trying to create a class that expands on the behavior of the boost graph library. I would like my class to be a template, where the user supplies a type (class) which will be used to store properties at every vertex. That's just background. I'm struggling with creating a more succinct typedef to use in defining my new class.

Based on other posts like this and this, I decided to define a struct that will contain the templated typedefs.

I'll show two closely related approaches. I can't figure out why the first typedef for GraphType seems to be working while the second for VertexType fails.

#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;
}

Compiler output:

$ 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’

Same thing, just avoiding use of GraphType in the second 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;
}

Compiler output looks effectively the same:

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’

Obviously the first compiler error is the root problem. I tried inserting typename in a few places with no success. I'm using gcc 4.2.1

How do I fix this?


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

Should fix it, I don't know where you tried to put it though.. you might have other problems I don't see.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜