需要帮助找出哪种语法适用于我的数据结构

时间:2013-12-19 16:56:58

标签: c++ list vector syntax adjacency-list

我正在尝试使用链接列表数组实现相邻列表:

vector< list<Edge> > adjList;

目前,我无法进行编译,因为我不太清楚如何访问

顶点甚至是我的嵌套类Edge的权重。这是错误输出......

Graph.cpp: In member function `void Graph::set_Edge(std::string, std::string, int)':
Graph.cpp:30: error: 'class std::list<Graph::Edge, std::allocator<Graph::Edge> >' has no member named 'm_vertex'
makefile.txt:9: recipe for target `Graph.o' failed
make: *** [Graph.o] Error 1

这里是Graph.h文件中的类声明(抱歉所有的评论,我喜欢把我的所有想法留在我的代码上,直到我准备将其打开...)

    #ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
//class MinPriority;
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
class Graph
{
public:
    Graph();
    ~Graph();
    /*void setArray(string vertex);
    void sortArray();
    Graph* getGraph(string preVertex, string vertex, int weight);
    void setGraph(string vertex, string postVertex, int weight);*/
    void set_Edge(string targetVertex, string vertex, int weight);
    friend class MinPriority;
private:
    class Edge
    {
    public:
        Edge(string vertex, int weight)
        {m_vertex = vertex; m_weight = weight;}
        ~Edge();
        string get_vertex(){}
        int get_weight(){}
    private:
        string m_vertex;
        int m_weight;
    };
    vector< list<Edge> > adjList;

};


#endif // GRAPH_H_INCLUDED

这是Graph.cpp

#include "Graph.h"

void Graph::set_Edge(string targetVertex, string vertex, int weight) //find target vertex
{
    for(unsigned int u = 0; u <= adjList.size(); u++)//traverses through the Y coord of the 2D array
    {
        if(targetVertex == adjList[u].m_vertex) // <<THIS IS WHERE THE ERROR OCCURS!!!!
        {
            adjList[u].push_back(Edge(vertex, weight)); //push new element to the back of the linked list at array index u.
        }

    }
    //we now need to add
    //adjList[adjList.size()].push_back(Edge(vertex, weight));
}

同样,我基本上需要帮助找出如何访问Edge的部分(顶点或重量)if(targetVertex == adjList[u].m_vertex)是我希望使用的思路,它会在数组中找到'u'索引并检查' u'索引的顶点元素并将其与目标顶点进行比较。

1 个答案:

答案 0 :(得分:1)

adjList的类型为list<Edge>,因此它没有任何名为m_vertex的成员。它包含的节点包含m_vertex个成员。

#include "Graph.h"

void Graph::set_Edge(string targetVertex, string vertex, int weight) 
{
    for(unsigned int u = 0; u <= adjList.size(); u++)
    {
        if(adjList[u].front().m_vertex == targetVertex)  
                   //^^add front() to access a list node 
                   //because the m_vertex member is same of all the edges in the list
                   //So just access the first one for simplicity.
        {
            adjList[u].push_back(Edge(vertex, weight)); 
        }
    }
}
相关问题