【问题标题】:List not declared in scope列表未在范围内声明
【发布时间】:2016-04-13 16:37:39
【问题描述】:

所以我试图打电话给我的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

【问题讨论】:

  • 会不会是“Edge.hpp”#includes“Vertex.hpp”?
  • 不是这样的。所有Edge.hpp 包括&lt;stdio.h&gt;
  • 您试图在Graph 的成员函数中使用Vertex 的成员变量?它们之间是什么关系?
  • Graph.hpp 包括Vertex.hppVertex.hpp 包括Edge.hpp

标签: c++ list class declare


【解决方案1】:

Graph::displayGraph() 中,您正在对Vertexs 的列表进行迭代。要从对象访问 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;
            }
        }
    }
}

【讨论】:

  • @TriskalJM,感谢您的编辑。现在好多了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
相关资源
最近更新 更多