列表未在范围内声明

时间:2016-04-13 16:37:40

标签: c++ list class declare

因此,我正在尝试拨打list Edge*名为edgelist的{​​{1}}。我下面有一个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

1 个答案:

答案 0 :(得分: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;
            }
        }
    }
}