iterator: not declared in this scope


B

Brice Rebsamen

I've had problem while using iterators:

vector<xxx>::iterator j;

the compiler reports
error: expected `;' before ‘j’
error: ‘j’ was not declared in this scope


The odd thing is that it does not fail all the time. For instance I
was using it without any problem while developing a Graph class. When
I modified the class to use templates for vertices and edges, I got
this problem.

I am declaring a side class Link<Vertice,Edge> and I want to use an
iterator on a vector of Link:
vector< Link<Vertice,Edge> >::iterator j;



Here is the exact code: See function remove

#include <vector>
using std::vector;

template <typename Vertice, typename Edge>
class Link {
public:
Edge *edge;
Vertice *vertice;
Link(Vertice *v=0, Edge *e=0) : vertice(v), edge(e) { }
};

template <typename Vertice, typename Edge>
class Node {
public:
Vertice *vertice;
vector< Link<Vertice,Edge> > links;
Node(Vertice *v=0) : vertice(v) { }
};

template <typename Vertice, typename Edge=int>
class Graph {
public:
virtual ~Graph() { }
void add(Vertice *v, Vertice *w=0, Edge *e=0);
void remove(Vertice *v);
void clear();

private:
vector< Node<Vertice,Edge> > nodes;
int searchVertice(const Vertice *v) const;
void get_routes(Tree<const Vertice *> &routes, const Vertice *end)
const;
};

template <typename Vertice, typename Edge>
int Graph<Vertice,Edge>::searchVertice(const Vertice *v) const {
for( int i=0; i<nodes.size(); i++ )
if( nodes.vertice==v )
return i;
return -1;
}

template <typename Vertice, typename Edge>
void Graph<Vertice,Edge>::add(Vertice *v, Vertice *w, Edge *e) {
int v_index = searchVertice(v);
if( v_index==-1 ) {
nodes.push_back(Node<Vertice,Edge>(v));
v_index = nodes.size()-1;
}

if( w!=0 ) {
int w_index = searchVertice(w);
if( w_index==-1 && w!=0 ) {
nodes.push_back(Node<Vertice,Edge>(w));
w_index = nodes.size()-1;
}

// link v & w with e
nodes[w_index].links.push_back(Link<Vertice,Edge>(v,e));
nodes[v_index].links.push_back(Link<Vertice,Edge>(w,e));
}
}

template <typename Vertice, typename Edge>
void Graph<Vertice,Edge>::remove(Vertice *v) {
int v_index = searchVertice(v);
if( v_index==-1 ) return;
vector< Link<Vertice,Edge> > & links = nodes[v_index].links;


//this is where the problem is
vector< Link<Vertice,Edge> >::iterator j;


for( j=links.begin(); j<links.end(); j++ ){
//for each connected node remove the connection to this node
Vertice *w = j->vertice;
int w_index = searchVertice(w);
if( w_index!=-1 ) {
vector< Link<Vertice,Edge> > & linksW = nodes[w_index].links;
vector< Link<Vertice,Edge> >::iterator l;
for( l=linksW.begin(); l<linksW.end(); l++ ) {
if( l->vertice==v ) {
linksW.erase(l);
}
}
}
//then delete the edge
if( j->edge ) delete j->edge;
//finally remove the link
links.erase(j);
}
}
 
Ad

Advertisements

M

Michael DOUBEZ

Brice Rebsamen a écrit :
I've had problem while using iterators:

vector<xxx>::iterator j;

the compiler reports
error: expected `;' before ‘j’
error: ‘j’ was not declared in this scope [snip]
template <typename Vertice, typename Edge>
class Link [snip]
//this is where the problem is
vector< Link<Vertice,Edge> >::iterator j;

You should indicate to the compiler that vector<T>::iterator is a typename.

Use:
typename vector< Link<Vertice,Edge> >::iterator j;


Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top