2016-04-13 120 views
0

所以我想打電話給我的listEdge* s叫做edgelist。我有一個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 s的列表。要從一個對象訪問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