[Lemon-user] Template Class of LEMON Graphs
Alpár Jüttner
alpar at cs.elte.hu
Mon Nov 19 08:55:02 CET 2012
Hi,
> I want to write a class, which does e.g. some calculations on given
> graphs (ListDigraph, FullDigraph or own derived LEMON-graphes). Now I
> have to write implementations for each graph type, which could be
> avoided via templates.
> Unfortunatly I am not very experienced with templates. How does a
> template access functions of the graph or even the NodeMaps, if a
> template doesn't know the type beforehand?
This is in fact a kind of stupidity of C++ and is independent from
LEMON. When you want to use a type defined by a template parameter, you
must tell the compiler that what you using is a type (or is a template
type). Don't ask me why the compiler cannot figure it out, it is
required by the C++ standard. Actually, the older versions of gcc was
able to do it, but newer versions are stricter in following the C++
standard, thus now they do not allow this kind of laziness.
See the examples on how to use sub-type declaration properly below.
> template < typename DGR> class outputClass {
> public:
> DGR &mGraph;
> DGR::NodeMap<int> mMap;
>
typename DGR:: template NodeMap<int> mMap;
or
typedef typename DGR:: template NodeMap<int> IntNodeMap;
IntNodeMap mMap;
>
> outputClass( const DGR &graph)
> : mGraph(graph), mMap(graph){};
>
> void output(){
> for (DGR::NodeIt n( mGraph ); n!=INVALID; ++n) {
for (typename DGR::NodeIt n( mGraph ); n!=INVALID; ++n) {
> std::cout << "value: " << mMap[ n ] << endl;
> }
> }
> };
I hope this helps.
Regards,
Alpar
More information about the Lemon-user
mailing list