How to iterate properly across a const set?

Posted by Jared on Stack Overflow See other posts from Stack Overflow or by Jared
Published on 2010-05-08T22:50:52Z Indexed on 2010/05/08 22:58 UTC
Read the original article Hit count: 190

Filed under:
|
|

I'm working on a program that's supposed to represent a graph. My issue is in my printAdjacencyList function. Basically, I have a Graph ADT that has a member variable "nodes", which is a map of the nodes of that graph. Each Node has a set of Edge* to the edges it is connected to. I'm trying to iterate across each node in the graph and each edge of a node.

void MyGraph::printAdjacencyList() {
std::map<std::string, MyNode*>::iterator mit;
std::set<MyEdge*>::iterator sit;

for (mit = nodes.begin(); mit != nodes.end(); mit++ ) {
    std::cout << mit->first << ": {";
    const std::set<MyEdge*> edges = mit->second->getEdges();
    for (sit = edges.begin(); sit != edges.end(); sit++) {
        std::pair<MyNode*, MyNode*> edgeNodes = *sit->getEndpoints();
    }
}
std::cout << " }" << std::endl;
}

getEdges is declared as:

const std::set<MyEdge*>& getEdges() { return edges; };

and get Endpoints is declared as:

const std::pair<MyNode*, MyNode*>& getEndpoints() { return nodes; };

The compiler error I'm getting is:

MyGraph.cpp:63: error: request for member `getEndpoints' in 
`*(&sit)->std::_Rb_tree_const_iterator<_Tp>::operator->
[with _Tp = MyEdge*]()', which is of non-class type `MyEdge* const'

MyGraph.cpp:63: warning: unused variable 'edgeNodes'

I have figured out that this probably means I'm misusing const somewhere, but I can't figure out where for the life of me. Any information would be appreciated. Thanks!

© Stack Overflow or respective owner

Related posts about c++

Related posts about const