Boost logo

Boost Users :

Subject: Re: [Boost-users] [BGL] Unable to sort list of edge_descriptors
From: Cedric Laczny (cedric.laczny_at_[hidden])
Date: 2010-11-06 06:54:49


Hi,

I am not absolutely sure on this, but it seems to me, that you have wrongly
specified your SortByName. The compiler accepts its code, when it is not used
(commented out), because it will not try to create an actual instance of the
code (formulation might be a bit simplistic). The syntax looks good to me, so
this should not be the problem.
But when the sort-algorithm wants to use this as functor for the comparison,
it will fail for several reasons.
First: when you look at your definition of SortByName, where should a functor
get the information of the Graph (g) from? You need to specify it as an
attribute so it can access the information on the edges in the graph.
Second: sort will expect an operator() that will use two elements that should
be compared, so specifying a Graph there will probably confuse it.
My suggestion for the struct would be:
   template < typename Graph >
     struct SortByName :
  {
            template< typename edgeDescriptor >
             bool operator()( const edgeDescriptor& a, const
 edgeDescriptor& b) const
           {
                 return g[a].eName < g[b].eName;
           }
 Graph g;
  };
And finally third, you have not specified a constructor or detailed constructor
in the struct nor did you specify the graph in your call of sort(). This is
important so the struct knows where to take the information from.

I am aware, that in my example, it is missing the "public
binary_function<edgeDescriptor,edgeDescriptor, bool>". I have to admit, that I
do not completely understand what this should do here. Probably to inherit
something?! This might be the reason why the compiler is complaining, as it
probably expects the two template-arguments here?

It would also be a bit easier to find out what is really causing the issue if
you could provide a more complete example so one can see the rest of the code.
A simple short main would be enough, especially as you have already defined
your functions separately.

Working with the BGL, I have experienced quite some situations where the
compiler was complaining about template-arguments, and such, that were
interenstingly caused by a completely different piece of code, so if my
suggestion does not fix the problem, please include a complete example.

Best,

Cedric

On Saturday, 6. November 2010 10:04:57 Mike Douglass wrote:
> I'm trying to sort a list of edge_descriptors, but I get the compiler error
> described below.
>
>
>
> typedef adjacency_list < vecS, vecS, bidirectionalS >::edge_descriptor
> edgeDescriptor; // defined globally
>
> template < typename Graph, typename edgeDescriptor >
> struct SortByName : public binary_function<edgeDescriptor,
> edgeDescriptor, bool>
> {
> bool operator()(const Graph & g, const edgeDescriptor& a, const
> edgeDescriptor& b)
> {
> return g[a].eName < g[b].eName;
> }
>
> };
>
>
> template < typename Graph >
> void Sort_Test(Graph & g)
> {
>
> typedef std::list<edgeDescriptor> edge_list;
> typename graph_traits<Graph>::edge_iterator edge_iter, edges_end;
>
> edge_list A;
>
> for (tie(edge_iter, edges_end) = edges(g); edge_iter != edges_end;
> ++edge_iter)
> A.push_back(*edge_iter);
>
> for (edge_list::iterator i = A.begin(); i != A.end(); ++i)
> cout << " *i = " << " " << g[*i].eName << "\n";
>
> // If the following line is commented out the program compiles and
> runs OK.
> A.sort(SortByName< typename Graph, typename edgeDescriptor >());
> //With the above line uncommented the compiler says:
>
> /*
> ./Includes/Utilities.cpp: In function ‘void Sort_Test(Graph&)’:
> ./Includes/Utilities.cpp:539: error: wrong number of template arguments (1,
> should be 2)
> ./Includes/Utilities.cpp:513: error: provided for ‘template<class Graph,
> class edgeDescriptor> struct SortByName’
> make: *** [plc] Error 1
> */
>
> }
>
> But clearly there are two template arguments - < typename Graph, typename
> edgeDescriptor >
>
> Thanks


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net