
Now another question arises: The vertex property writer is working, but if I add the edge property writer, it compiles error again: $ g++ main.cpp /usr/local/include/boost/graph/graphviz.hpp: In function `void boost::write_graphviz(std::ostream&, const Graph&, VertexPropertiesWriter, EdgePropertiesWriter, GraphPropertiesWriter) [with Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, BasicOperator, boost::no_property, boost::no_property, boost::listS>, VertexPropertiesWriter = schedule_vertex_writer<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, BasicOperator, boost::no_property, boost::no_property, boost::listS> >, EdgePropertiesWriter = schedule_edge_writer<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, BasicOperator, boost::no_property, boost::no_property, boost::listS> >, GraphPropertiesWriter = boost::default_writer]': /usr/local/include/boost/graph/graphviz.hpp:300: instantiated from `void boos ::write_graphviz(std::ostream&, const Graph&, VertexWriter, EdgeWriter) [with G aph = Graph, VertexWriter = schedule_vertex_writer<boost::adjacency_list<boost: vecS, boost::vecS, boost::bidirectionalS, BasicOperator, boost::no_property, bo st::no_property, boost::listS> >, EdgeWriter = schedule_edge_writer<boost::adja ency_list<boost::vecS, boost::vecS, boost::bidirectionalS, BasicOperator, boost :no_property, boost::no_property, boost::listS> >]' main.cpp:51: instantiated from here /usr/local/include/boost/graph/graphviz.hpp:270: error: no match for call to `( schedule_edge_writer<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, BasicOperator, boost::no_property, boost::no_property, boost::listS> >) (std::basic_ostream<char, std::char_traits<char> >&, boost::detail::edge_desc_impl<boost::bidirectional_tag, size_t>)' helper.hpp:73: error: candidates are: void schedule_edge_writer<Graph>::operator()(std::ostream&, Edge&) [with Edge = boost::detail::edge_desc_impl<boost::bidirectional_tag, size_t>, Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, BasicOperator, boost::no_property, boost::no_property, boost::listS>] I use the same pattern for vertex property writer and edge property writer, here is the code snapshot: //Class template of a vertex property writer template < typename Graph > class schedule_vertex_writer { public: schedule_vertex_writer(Graph _g) : g(_g) {} template <typename Vertex> void operator()(std::ostream& out, Vertex& v) { out << "[label=\"" << g[v].toString() << "\"]"; } private: Graph& g; }; //Function template to return an object of the above class template <typename Graph> inline schedule_vertex_writer<Graph> make_schedule_vertex_writer(Graph g){ return schedule_vertex_writer<Graph>(g); } //Class template of an edge property writer template < typename Graph > class schedule_edge_writer { public: schedule_edge_writer(Graph _g) : g(_g) {} template <typename Edge> void operator()(std::ostream& out, Edge& v) { out << "[label=\"" << "E" << "\"]"; } private: Graph& g; }; //Function template to return an object of the above class template <typename Graph> inline schedule_edge_writer<Graph> make_schedule_edge_writer(Graph g){ return schedule_edge_writer<Graph>(g); } -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Welson Sun Sent: Friday, February 11, 2005 3:19 PM To: boost-users@lists.boost.org Subject: RE: [Boost-users] BGL problem,originally [Another C++inheritence/polymorphism problem] Yeah, if I change the vertex property writer to be: void operator()(std::ostream& out, Vertex& v) It works fine. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Douglas Gregor Sent: Friday, February 11, 2005 6:23 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] BGL problem,originally [Another C++ inheritence/polymorphism problem] On Feb 10, 2005, at 10:25 PM, Welson Sun wrote:
Well, I found out that this is not a problem related to inheritence nor polymorphism, it is a problem with the BGL
1. Even if I make BasicOperator not inherit from Operator, the problem is the same. Here is the modified BasicOperator:
class BasicOperator /* : public Operator */{ public: BasicOperator() {}; BasicOperator(OperatorType* type) : _type(type) {toString = _type->getMnemonic();}; BasicOperator(OperatorType* type, string cust) : _type(type), _cust(cust) {toString = _type->getMnemonic() + "&" + _cust;}; OperatorType* getOperatorType() { return _type; } string toString() { return toString; } string toString; private: OperatorType* _type; string _cust; };
And the vertex property writer is: void operator()(std::ostream& out, const Vertex& v) const { out << "[label=\"" << "(" << g[v].toString() << ")" << "\"]"; }
The error is: main.cpp:52: error: passing `const BasicOperator' as `this' argument of ` std::string BasicOperator::toString()' discards qualifiers
Looking at the code you previously posted and mapping that to what you've written here, here's what happens: schedule_vertex_writer stores a _copy_ of the graph (called g) inside it. Since operator() is marked "const", g[v] returns a reference to a const BasicOperator. But, when you call toString(), it is not marked const, so you get the error above. I have two suggestions: (1) Pass and store the graph g as a "const Graph&" in schedule_vertex_writer. Copying graphs can be really expensive. (2) Make toString() const. If you absolutely cannot do this, then you'll need to store a non-const reference to the graph instead of a const one.
So it looks like that the problem is related to the "const" BasicOperator I passed into std::string BasicOperator::toString(). How can I fix this? Which means if the property of a vertex is a Class, how can I call its member function in the vertex writer?
2. If I changes the vertex property writer as: void operator()(std::ostream& out, const Vertex& v) const { out << "[label=\"" << "(" << g[v].toString << ")" << "\"]"; } Everything is OK. Which mean in the vertex property writer, I can access the data member without any problem, but I cannot access the member function.
Right, because data members can be accessed irrespective of the cv-qualifiers on the object type. You'll just get a const std::string& back. Doug _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users