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
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
{ 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
struct SortByName’ make: *** [plc] Error 1 */ }
But clearly there are two template arguments - < typename Graph, typename edgeDescriptor >
Thanks