简体   繁体   中英

How do I get an Iterator over a vector of objects from a Template?

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

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 eg Comeaus template FAQ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM