[BGL] Different types as vertices of a graph
I'd like to use the graph library to support connections between different types of objects. To do this I've created a class GraphNode that forms the base class for various other objects that can become vertices of the graph. I've tried the following. int main(int,char*[]) { //these two classes Translate and Mesh inherit from GraphNode Translate* pT = new Translate; Mesh* pM = new Mesh; // create a typedef for the Graph type typedef adjacency_list<vecS, vecS, bidirectionalS> Graph; const int num_vertices = 2; // writing out the edges in the graph typedef std::pair<GraphNode*, GraphNode*> GraphEdge; GraphEdge edge_array[] = { GraphEdge(pT,pM) }; const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]); // declare a graph object Graph g(num_vertices); // add the edges to the graph object for (int i = 0; i < num_edges; ++i) add_edge(edge_array[i].first, edge_array[i].second, g); return 0; } but get the following error message related to the fact that it can't choose a suitable overloaded add_edge function. If I replace the types in the vertex with an int it compiles happily. I'm using Boost 1.30.0 e:\Code\Edge\Console\Console.cpp(42) : error C2665: 'boost::add_edge' : none of the 2 overloads can convert parameter 1 from type 'GraphNode *' e:\Code\boost\boost\graph\detail\adjacency_list.hpp(1211): could be 'std::pair<_Ty1,_Ty2> boost::add_edge<boost::detail::adj_list_gen<Graph,VertexListS,OutEdgeListS,DirectedS,VertexProperty,EdgeProperty,GraphProperty,EdgeListS>::config>(boost::detail::adj_list_gen<Graph,VertexListS,OutEdgeListS,DirectedS,VertexProperty,EdgeProperty,GraphProperty,EdgeListS>::config::vertex_descriptor,boost::detail::adj_list_gen<Graph,VertexListS,OutEdgeListS,DirectedS,VertexProperty,EdgeProperty,GraphProperty,EdgeListS>::config::vertex_descriptor,boost::bidirectional_graph_helper_with_property<Config> &)' with [ _Ty1=boost::adj_list_helper<boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS>,boost::vecS,boost::vecS,boost::bidirectionalS,boost::no_property,boost::no_property,boost::no_property,boost::listS>::config,boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS>,boost::vecS,boost::vecS,boost::bidirectionalS,boost::no_property,boost::no_property,boost::no_property,boost::listS>::DirectedHelper>::edge_descriptor, _Ty2=bool, Graph=boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS>, VertexListS=boost::vecS, OutEdgeListS=boost::vecS, DirectedS=boost::bidirectionalS, VertexProperty=boost::no_property, EdgeProperty=boost::no_property, GraphProperty=boost::no_property, EdgeListS=boost::listS, Config=boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS>,boost::vecS,boost::vecS,boost::bidirectionalS,boost::no_property,boost::no_property,boost::no_property,boost::listS>::config ] e:\Code\boost\boost\graph\detail\adjacency_list.hpp(1941): or 'std::pair<_Ty1,_Ty2> boost::add_edge<Graph,boost::detail::adj_list_gen<Graph,VertexListS,OutEdgeListS,DirectedS,VertexProperty,EdgeProperty,GraphProperty,EdgeListS>::config,boost ::detail::adj_list_gen<Graph,VertexListS,OutEdgeListS,DirectedS,VertexProperty,EdgeProperty,GraphProperty,EdgeListS>::DirectedHelper>(boost::detail::adj_list_gen<Graph,VertexListS,OutEdgeListS,DirectedS,VertexProperty,EdgeProperty,GraphProperty,EdgeListS>::config::vertex_descriptor,boost::detail::adj_list_gen<Graph,VertexListS,OutEdgeListS,DirectedS,VertexProperty,EdgeProperty,GraphProperty,EdgeListS>::config::vertex_descriptor,boost::vec_adj_list_impl<Graph,Config,Base> &)' with [ _Ty1=boost::adj_list_helper<boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS>,boost::vecS,boost::vecS,boost::bidirectionalS,boost::no_property,boost::no_property,boost::no_property,boost::listS>::config,boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS>,boost::vecS,boost::vecS,boost::bidirectionalS,boost::no_property,boost::no_property,boost::no_property,boost::listS>::DirectedHelper>::edge_descriptor, _Ty2=bool, Graph=boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS>, VertexListS=boost::vecS, OutEdgeListS=boost::vecS, DirectedS=boost::bidirectionalS, VertexProperty=boost::no_property, EdgeProperty=boost::no_property, GraphProperty=boost::no_property, EdgeListS=boost::listS, Config=boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS>,boost::vecS,boost::vecS,boost::bidirectionalS,boost::no_property,boost::no_property,boost::no_property,boost::listS>::config, Base=boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS>,boost::vecS,boost::vecS,boost::bidirectionalS,boost::no_property,boost::no_property,boost::no_property,boost::listS>::DirectedHelper ] while trying to match the argument list '(GraphNode *, GraphNode *, Graph)' e:\Code\boost\boost\iterator_adaptors.hpp(1409) : see reference to class template instantiation 'boost::filter_iterator_generator<Predicate,Iterator,Value,Reference,Pointer,Category,Distance>' being compiled
Hi, First, to address why what you're doing doesn't work, then on to an alternative. The add_edge function is expecting arguments of type graph_traits<Graph>::vertex_descriptor. Another words, you get a compiler error because there is no add_edge function defined for your GraphNode class. As for an alternative, do you really need the node to be a base class, or could you get by with attaching a property to each node, where the property is a base class. The reason I say this is that the BGL adjacency_list graph lets the user specify property types, but not the vertex_descriptor type. Another option, of course, would be to implement your own graph class. Cheers, Jeremy
participants (2)
-
Jeremy Siek
-
sashan