Hello everyone, my first post here, so I hope not to miss some mailing list rule. I'm experiencing some problems in using graph internal properties. I have to generate a graph from a database and I need to differentiate nodes in my graph, namely I will have 2 different node categories, say cat1_node and cat2_node. Each category should have its own property attached. Ex: say N1 is a cat1_node and N2 is a cat2_node. I want N1 to have a "name" (string) property while N2 a "length" (int) property and both have a "label" (string) property. To achieve this I though I could use inheritance in the following manner: //FATHER PROPERTY CLASS class generic_vertex_property{ public: generic_vertex_property(){type = 0;} int type; std::string label; }; //SON PROPERTY CLASS class cat1_property: public generic_vertex_property{ public: std::string name; }; //SON PROPERTY CLASS class cat2_property: public generic_vertex_property{ public: int length; }; In this way, I though, I can define my graph something like: typedef adjacency_list< vecS, vecS, directedS, property<vertex_p_t, generic_vertex_property*> > Graph; and add new vertices like: cat1_property *v1 = new cat1_property(); cat2_property *v2 = new cat2_property(); v1->label = "v1_label"; v1->name = "v1_name"; v2->label = "v2_label"; v2->length = 10; add_vertex(VertexProperty(v1),G); add_vertex(VertexProperty(v2),G); This actually compiles and runs without errors. The problem comes when I try to access the verteces through a property_map, like: vertex_p_map_t vertex_p_map = get(vertex_p, G); //where vertex_p_map_t and vertex_p have been define before Indeed from such get function I have objects of type generic_vertex_property, thus, when trying to access a property of the sone classes I got this compile error: error: ‘class generic_vertex_property’ has no member named ‘name’ I hope I was clear enough, if necessary I can also send the full code. In the meanwhile here are my questions: Is it possible to do what I want to do? Is there a way to get a property map with the "original" object types (as inserted)? Do you have some good websites with practical examples, code snippets and so on about BGL? Last question comes as, in my opinion, the reference manual is quite complete, but 1) lot of examples are not compiling ( look like they have not been updated to the last boost version) 2) examples are usually very basic and it is very hard to get the general rules behind it. hope someone can help me out with this. bests, Paolo
On Oct 10, 2011, at 10:00 PM, Paolo Fogliaroni <paolo@informatik.uni-bremen.de> wrote:
I'm experiencing some problems in using graph internal properties.
This isn't a Boost Graph issue, this is a C++ issue.
typedef adjacency_list< vecS, vecS, directedS, property<vertex_p_t, generic_vertex_property*> > Graph; ...
Indeed from such get function I have objects of type generic_vertex_property, thus, when trying to access a property of the son classes I got this compile error: error: ‘class generic_vertex_property’ has no member named ‘name’
Yes, the compiler cannot assume that any given instance of generic_vertex_property might also be an instance of a subclass. If you can conceptualize the problem as an operation that you need to perform on every vertex, but implemented in two different ways depending on the vertex subclass type, you could give the base class and its subclasses a virtual method. Put your access logic into that method, so that code needing to access 'name' is implemented in the subclass that defines 'name'. If you can't approach it that way, you might try dynamic_cast with pointers - but this isn't the preferred approach. (Imagine adding a third property subclass...) Look up virtual methods and dynamic_cast.
participants (2)
-
Nat Goodspeed
-
Paolo Fogliaroni