将矢量容器传递给其他函数

时间:2012-12-09 15:24:58

标签: c++ list graph vector nodes

这个问题是this one的连续统一体 我的Node类中有一个列表向量,但没有传递给其他函数。

这是Node.h

#include "MyGraph.h"

//The Node Class will take the information from the graph class and
//convert the information into a format so we can perform DFS on our
//graph.

enum VertexState { White, Gray, Black };


class Node {
    private:
    string nodeId;
    VertexState status; //whether or not the vertex Node has been visited or not
    vector<Node> nodeList;
    vector<list<Node> > *edgeList;

public:
    Node(): nodeId(), status(){}

    //copy constructor
    Node(string vertex){
    nodeId=vertex;
    status = White;
    }
    ~Node(){}

   //Setter and Getter methods for our class variables
    void setId(string vertex){
       nodeId = vertex;
    }
    void setStatus(VertexState newStatus){
        status = newStatus;
    }

    string getNodeId(){
        return nodeId;
    }

    VertexState getStatus(){
        //status == White then it is not visited
        //status == Gray its being processed
        //status == Black then it has been visited
        return status;

    }


    vector<Node> getNodeList(){
        return nodeList;
    }
    vector<list<Node> > getEdgeList(){
        return *edgeList;
    }



    //create nodes from vertex list in the graph object
    void createNodeList(MyGraph graphObject){
        vector<string> vertexList;
        vertexList = graphObject.getVertexList();
        vector<string>::iterator it;
        vector<Node>placeNodeList;
        for (it = vertexList.begin(); it !=vertexList.end(); it++){
            Node newNode(*it);
            placeNodeList.push_back(newNode);
        }

        nodeList = placeNodeList;

        cout << "Size of initial nodeList: " << nodeList.size()<<endl;
    }


    //creates container for edge lists from the graph object
    void createEdgeList(MyGraph graphObject){
        vector<list<string> > newEdgeList;
        newEdgeList = graphObject.getEdgeList();



        vector<list<Node> > myEdgeList;


        vector<list<string> >::iterator it;
        for (it = newEdgeList.begin() ; it != newEdgeList.end(); it++) {
            list<string> edgeString;
            list<string>::iterator eIt;
            edgeString =*it;
            list<Node> edgeContainer; //creates a list container to be pushed on to our edgeList variable
            for (eIt = edgeString.begin(); eIt != edgeString.end(); eIt++) {
                Node newNode(*eIt);
                edgeContainer.push_back(newNode);
            }
            myEdgeList.push_back(edgeContainer);
        }
        edgeList = &myEdgeList;

        cout << "Size of intial edgeList: "<<edgeList->size() <<endl;




    }




    //The operational methods that will work on the Nodes of the graph
    vector<Node> dephtFirstSearch(Node vertex);//will determine if our graph is connected.
    vector<Node> DFS2(vector<Node> copyNodeList);
    bool isGraphConnected(vector<Node> nodeList);
    bool isEdgeConnected(Node vertex1, Node vertex2);//will determine if there is an edge between two nodes
    bool findElementaryCycles();
    void findArticulationPoints();
    void removeVertex(Node myNode, vector<Node>copyNodeList, vector<list<Node> >copyEdgeList);

    };

当我尝试使用以下函数在我的其他函数中调用vector edgeList时:     edgeList-&GT;空; 该函数不应该是空的。 每次我需要使用它时都会调用createEdgeList函数,因此它不为空。 如何将私有变量中的数据传递给函数?

1 个答案:

答案 0 :(得分:0)

vector<list<Node> > myEdgeList;    
...
edgeList = &myEdgeList;

您正在存储指向局部变量的指针 - 这最多是未定义的行为。

为什么不将边缘列表作为法线向量并直接在createEdgeList

中添加