Re: Re: [Boost-users] newbie: Graph library - bundled properties revisited
Date: Wed, 24 Nov 2004 00:07:08 -0500 From: Doug Gregor <dgregor@cs.indiana.edu> Subject: Re: [Boost-users] newbie: Graph library - bundled properties revisited To: boost-users@lists.boost.org Message-ID: <B11939FE-3DD6-11D9-9E73-000D932B7224@cs.indiana.edu> Content-Type: text/plain; charset=US-ASCII;
Still has a compile problem. interior_property_map.cpp is now: struct vertexStruct { string name; }; template <class EdgeIter, class Graph> void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G) { // Access the propety acessor type for this graph typedef typename property_map<Graph, &vertexStruct::name>::const_type NamePA; // typedef typename property_map<Graph, vertexStruct::name>::const_type NamePA; NamePA name = get(&vertexStruct, G); typedef typename boost::property_traits<NamePA>::value_type NameType; NameType src_name, targ_name; while (first != last) { src_name = G[source(*first, G)].name; targ_name = G[target(*first, G)].name; cout << src_name << " owes " << targ_name << " some money" << endl; ++first; } } int main() { { // Create the graph, and specify that we will use std::string to // store the first name's. typedef adjacency_list<vecS, vecS, directedS, vertexStruct> MyGraphType; typedef pair<int,int> Pair; Pair edge_array[11] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1), Pair(3,4), Pair(4,0), Pair(4,1) }; MyGraphType G(5); for (int i=0; i<11; ++i) add_edge(edge_array[i].first, edge_array[i].second, G); MyGraphType::vertex_descriptor v = *vertices(G).first; G[v].name = "TingChong"; ++v; G[v].name = "Carmen"; who_owes_who(edges(G).first, edges(G).second, G); } cout << endl; return 0; } Compiler output is: gcc-C++-action /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.test/gcc/debug/inlining-on/interior_property_map.o /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp: In function `void who_owes_who(EdgeIter, EdgeIter, const Graph&)': /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:59: error: type/value mismatch at argument 2 in template parameter list for `template<class Graph, class Property> struct boost::property_map' /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:59: error: expected a type, got `& vertexStruct::name' /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:59: error: `const_type' does not name a type /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:64: error: `NamePA' undeclared (first use this function) /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:64: error: (Each undeclared identifier is reported only once for each function it appears in.) /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:64: error: expected `;' before "name" /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:66: error: template argument 1 is invalid /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:66: error: `value_type' does not name a type /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:68: error: `NameType' undeclared (first use this function) /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:68: error: expected `;' before "src_name" /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:71: error: `src_name' undeclared (first use this function) /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp:72: error: `targ_name' undeclared (first use this function) set -e "g++" -c -Wall -ftemplate-depth-255 -g -O0 -Wno-inline -I"../../../bin/boost/libs/graph/example" -I "/home/tcma/cpp/boost_1_32_0" -o "/home/tcma/cpp/boosttcma/libs/graph/interior_property_map.test/gcc/debug/inlining-on/interior_property_map.o" "/home/tcma/cpp/boosttcma/libs/graph/interior_property_map.cpp" "/usr/bin/objcopy" --set-section-flags .debug_str=contents,debug "/home/tcma/cpp/boosttcma/libs/graph/interior_property_map.test/gcc/debug/inlining-on/interior_property_map.o" ...failed gcc-C++-action /home/tcma/cpp/boosttcma/libs/graph/interior_property_map.test/gcc/debug/inlining-on/interior_property_map.o... ...skipped </home/tcma/cpp/boosttcma/libs/graph/interior_property_map.test/gcc/debug/inlining-on>/home/tcma/cpp/boosttcma/libs/graph/interior_property_map for lack of </home/tcma/cpp/boosttcma/libs/graph/interior_property_map.test/gcc/debug/inlining-on>interior_property_map.o... ...skipped </home/tcma/cpp/boosttcma/libs/graph/interior_property_map.test/gcc/debug/inlining-on>/home/tcma/cpp/boosttcma/libs/graph/interior_property_map.run for lack of </home/tcma/cpp/boosttcma/libs/graph/interior_property_map.test/gcc/debug/inlining-on>/home/tcma/cpp/boosttcma/libs/graph/interior_property_map... ...failed updating 1 target... ...skipped 3 targets... format=flowed
On Nov 23, 2004, at 10:56 PM, TC MA wrote:
// create a tag for our new property
enum vertex_first_name_t { vertex_first_name }; namespace boost { BOOST_INSTALL_PROPERTY(vertex, first_name); }
You won't actually need the part above this any
more.
struct vertexStruct { string name; };
template <class EdgeIter, class Graph> void who_owes_who(EdgeIter first, EdgeIter last,
Graph& G) { // Access the propety acessor type for this graph typedef typename property_map<Graph, vertexStruct::name>::const_type NamePA;
The compiler is actually giving a somewhat useful message here: add an '&' before the vertexStruct::name to form a
const pointer-to-member.
NamePA name = get(vertex_first_name, G);
Here again, you'll want to use a pointer-to-member,
like this:
NamePA name = get(&vertexStruct, G);
typedef typename boost::property_traits<NamePA>::value_type
NameType src_name, targ_name;
while (first != last) { src_name = boost::get(name, source(*first,G)); targ_name = boost::get(name,
target(*first,G));
Now you can use the simpler:
src_name = G[source(*first, G)].name; targ_name = G[target(*first, G)].name;
Once you've replaced the original get calls with
NameType; these lines, you can
remove the definition of the "name" variable as well.
Doug
===== TingChong Ma ______________________________________________________________________ Post your free ad now! http://personals.yahoo.ca
On Nov 24, 2004, at 11:21 AM, TC MA wrote:
Still has a compile problem. interior_property_map.cpp is now:
struct vertexStruct { string name; };
template <class EdgeIter, class Graph> void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G) { // Access the propety acessor type for this graph typedef typename property_map<Graph, &vertexStruct::name>::const_type NamePA;
My mistake, this would have needed to be: typedef typename property_map<Graph, string (vertexStruct::*)>::const_type NamePA; But, this shows us just how different bundled properties are from the prior types of properties. The property_map class becomes harder to use, but in many cases is not necessary. At the end is my rewrite of the example, which illustrates how much less baggage we need to worry about when we go all-out with bundled parameters. Doug template <class EdgeIter, class Graph> void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G) { while (first != last) { cout << G[source(*first, G)].first_name << " owes " << G[target(*first, G)].first_name << " some money" << endl; ++first; } } struct VertexData { string first_name; }; int main() { { // Create the graph, and specify that we will use std::string to // store the first name's. typedef adjacency_list<vecS, vecS, directedS, VertexData> MyGraphType; typedef pair<int,int> Pair; Pair edge_array[11] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1), Pair(3,4), Pair(4,0), Pair(4,1) }; MyGraphType G(5); for (int i=0; i<11; ++i) add_edge(edge_array[i].first, edge_array[i].second, G); G[0].first_name = "Jeremy"; G[1].first_name = "Rich"; G[2].first_name = "Andrew"; G[3].first_name = "Jeff"; G[4].first_name = "Doug"; who_owes_who(edges(G).first, edges(G).second, G); } cout << endl; return 0; }
participants (2)
-
Doug Gregor
-
TC MA