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