How do I pass an object by reference inside parame

2019-09-02 03:36发布

In order to get...

void MinPriority::createArray(string targetVertex, Graph & graph)
{
    vector <list <Graph::Edge> >& adjList = graph.get_adjList();
}

to work I need to pass in Graph &graph by reference from another function:

void Graph::MST_PRIM()
{
    MinPriority priority;
    for(unsigned int i = 0; i != adjList.size(); i++)
    {
        priority.createArray(adjList[i].front().m_vertex, /*...*/);
    }
}

what would i put into /*...*/ to get createArray to work? Here is a rough example of what class Graph looks like:

class Graph
{
private:
    vector< list<Edge> > adjList;
public:
    Graph();
    ~Graph();
    class Edge
    {
    public:
        Edge(string vertex, int weight)
        {
            m_vertex = vertex;
            m_weight = weight;
        }
        ~Edge(){}
        string m_vertex;
        int m_weight;
    };

    vector < list < Edge > > get_adjList(){return adjList;}
    //Other functions....

};

Please notify me if you need any more information. Basically I want to access adjList by reference and use it within createArray to make a vector out of it. (in a separate class of course)

1条回答
淡お忘
2楼-- · 2019-09-02 04:16

It seems all you need is to pass in *this:

priority.createArray(adjList[i].front().m_vertex, *this);
查看更多
登录 后发表回答