0

这个问题是这个问题的一个连续体
我的 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);

    };

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

4

1 回答 1

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

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

为什么不将 edgelist 设为法线向量并直接添加到它createEdgeList

于 2012-12-09T15:27:19.963 回答