void Graph::insertEdge(int v1, int v2, double w) { bool status = false; while (!status) { if (v1==-1 || v2==-1) { cout << "the end node (" << vertexNames[v1] << " or " << vertexNames[v2] << ") of the input link does not exist." << endl; status = true; break; } for (list<EdgeType>::iterator iter= adjMap[v1].begin(); // 这里编译无误 iter != adjMap[v1].end(); iter++) { if (iter->adjvex == v2) { cout << "The link <"<< vertexNames[v1] << "," << vertexNames[v2] <<"> has already exist." << endl; status = true; break; } else cout << "the neighbour of " << vertexNames[v1] << " is " << vertexNames[iter->adjvex] << "; "; } cout << endl; countEdges++; adjMap[v1].push_back(EdgeType(v2, w, countEdges)); if (!directed) adjMap[v2].push_back(EdgeType(v1, w, countEdges)); status = true; } }
但是当其他成员function中也需要使用 类似循环的时候 for (list<EdgeType>::iterator iter = adjMap[u].begin()..... 编译出错:
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion)