views:

73

answers:

1

I'm busy implementing a Graph ADT in C++. I have templates for the Edges and the Vertices. At each Vertex I have a vector containing pointers to the Edges that are incident to it. Now I'm trying to get an iterator over those edges. These are the lines of code:

vector<Edge<edgeDecor, vertexDecor, dir>*> edges = this->incidentEdges();
vector<Edge<edgeDecor, vertexDecor, dir>*>::const_iterator i;
for (i = edges.begin(); i != edges.end(); ++i) {

However, the compiler won't accept the middle line. I'm pretty new to C++. Am I missing something? Why can't I declare an iterator over objects from the Edge template? The compiler isn't giving any useful feedback.

Much thanks niel

+3  A: 

If that snippet comes from a template, you have probably run into the problem of dependent names - use typename:

typename vector<Edge<edgeDecor, vertexDecor, dir>*>::const_iterator i;

typename tells the compiler that you are referring to a type. Without it, dependent names are assumed to not be types or templates.

For more details have a look at e.g. Comeaus template FAQ.

Georg Fritzsche
I doubt that `typename` keyword is necessary here. Edge is used as a class (it is fully specialized), not as a template here as I can see.
Kirill V. Lyadvinsky
I am assuming that at least one of the template arguments to `Edge<>` is a template argument to the surrounding template - clarified that.
Georg Fritzsche
Thanks gf, that seems to solve it, it compiles fine now. I haven't tested the code yet but I'm pretty confident that's the last of that. For those who are interested I pasted the two header files, as well as the cpp file in question at http://pastebin.com/dR3S78Pv (unaltered).
nieldw
Looks a bit different, but you definitely have issues with dependent names there. Also note that you can't split declaration and definition of templates unless you explicitly instantiate them for all needed types. Thus templates are usually completely implemented in headers.
Georg Fritzsche