开发者

Passing type of template function to template class used within it?

开发者 https://www.devze.com 2023-01-13 01:05 出处:网络
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:

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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消