0

この 2、3 時間、私はこの問題に取り組んできましたが、運がありませんでした。テキストを読んで、すべての頂点がリスト内の隣接する頂点へのリンクを持つ隣接リストを作成しようとしています。頂点クラスは次のようになります。

class Vertex {
private:
  std::vector<Vertex*> connPtrVertices;  // vector of vertices adjacent to this one
public:
  void addVertex(Vertex* vert) { connPtrVertices.push_back(vert); }

そして main.cpp では、次のように頂点を互いに接続しようとしています:

Vertex *v1, *v2;
v2->addVertex(v1);    // connect v2 to v1 
v1->addVertex(v2);    // connect v1 to v2 

次のような Debug Assertion Failed メッセージが表示されます。

Debug Assertion Failed!

Program: C:\Windows\system32\MSVCP110D.dll
File: c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector
Line: 240

Expression: vector iterators incompatible

どうすればいいのかわかりません。どんな助けでも大歓迎です。

編集: ループを介して初めて v1 と v2 に new を割り当てますが、2 回目はそれらが存在するかどうかを確認し、存在する場合は次のように頂点にポインターを割り当てます。

v1 = &(p_graph->getVertex(vert1));

呼び出されるメソッドは次のとおりです。

Vertex Graph::getVertex(std::string v) {         // gets the vertex 
  for (std::vector<Vertex>::iterator it = vertices.begin(); it != vertices.end(); it++) {
     if ((it->getName()).compare(v) == 0)
        return *it;  // if strings are the same return vertex
    }
    exit(1);
 }

間違いはここですか?

4

1 に答える 1