views:

40

answers:

1

I am trying to build a template function. Inside templated classes are used and I would like to pass the template type of the function to those classes. So I have:

template <class T>
T find_bottleneck (ListGraph &g, CrossRefMap<ListGraph, Edge, T> &weight, Node &s, Node &t) {
    // Check if theres a single edge left
    if (countEdges(g) == 1) {
        CrossRefMap<ListGraph, Edge, T>::ValueIt itr = weight.beginValue();
        return *itr;
    }

However this fails, citing

lemon_graph.cpp: In function ‘T find_bottleneck(lemon::ListGraph&, lemon::CrossRefMap<lemon::ListGraph, lemon::ListGraphBase::Edge, T>&, Node&, Node&)’:
lemon_graph.cpp:20: error: expected ‘;’ before ‘itr’
lemon_graph.cpp:21: error: ‘itr’ was not declared in this scope

I tried to recreate this using a simple example of a function which generates vectors based on the type passed to it and that compiled fine, so I am not sure what the problem is here.

+3  A: 

It's just a missing typename.

typename CrossRefMap<ListGraph, Edge, T>::ValueIt

typename is the answer to at least 50% of all C++-template-related questions :-) It tells the compiler that what follows is always a type, regardless of the template parameters (ValueIt could for example be a int instead of a typedef for an iterator).

Alexander Gessler
ah ok, thanks! I thought it was typename but I was tryingCrossRefMap<ListGraph, Edge, typename T>::ValueIt
zenna
@zenna: The rule is to always consider typename when you see `::`. typename is always applied to the "result" of an `::` operator.
Potatoswatter