2016-04-13 5 views
0

というlistEdge*を呼び出しようとしています。私はgraph.cppのグラフの隣接リストを表示するはずです。範囲内に宣言されていないリスト

#include <sstream> 
#include <fstream> 
#include <iostream> 
#include <string> 
#include <list> 
#include "Graph.hpp" 

Graph::Graph(){} 

void Graph::displayGraph(){ 
for(int i = 0; i < vertices[vertices.size()-1].label; i++){ 
    cout << vertices[i].label << ": "; 
    for(int j = 0; j <= edgeList.size(); j++){ 
     if(edgeList[j].start==i){ 
      cout << edgeList[j].end; 
     } 
    } 
} 
} 
Graph.hpp

は以下であるVertex.hppを含みます。

#ifndef Vertex_hpp 
#define Vertex_hpp 

#include <stdio.h> 
#include <list> 
#include <string> 
#include <vector> 

#include "Edge.hpp" 
using namespace std; 

class Vertex { 
public: 
// the label of this vertex 
int label; 
// using a linked-list to manage its edges which offers O(c) insertion 
list<Edge*> edgeList; 

// init your vertex here 
Vertex(int label); 

// connect this vertex to a specific vertex (adding edge) 
void connectTo(int end); 

}; 
#endif /* Vertex_hpp */ 

ただし、コードを実行すると、エラーedgeList is not declared in this scopeが発生します。

+0

"Edge.hpp"#include's "Vertex.hpp"はありますか? – nwp

+0

それはそうではありません。すべての 'Edge.hpp'が' ' –

+1

です。 'Graph'のメンバ関数で' Vertex'のメンバ変数を使用しようとしていますか?彼らの関係は何ですか? – songyuanyao

答えて

0

Graph::displayGraph()では、Vertexのリストを繰り返しています。オブジェクトからedgeListフィールドにアクセスするには、そのオブジェクトを参照する必要があります。次のコードを参照してください。

void Graph::displayGraph(){ 
    for(int i = 0; i < vertices[vertices.size()-1].label; i++){ 
     cout << vertices[i].label << ": "; 
     for(int j = 0; j <= vertices[i].edgeList.size(); j++){ 
      if(vertices[i].edgeList[j].start==i){ 
       cout << vertices[i].edgeList[j].end; 
      } 
     } 
    } 
} 
+0

@TriskalJM、ありがとう。今それははるかに良いです – Ivan

関連する問題