2012-12-09 47 views
0

來自這個問題this one
一個統一體我有一個列表的向量在我的節點類沒有被傳遞給其他的功能。傳遞載體容器等功能

這裏是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->空; 該函數不應該是空的。 createEdgeList函數每次需要使用時都會調用,所以它不是空的。 如何將數據從我的私有變量傳遞給函數?

+0

http://stackoverflow.com/questions/13783090/sigbart-error-vectorlistmyclass沒有添加對不起鏈接 – danielBB

+0

固定爲您 – Default

+2

添加到它爲什麼o edgeList是一個裸指針? – KillianDS

回答

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

您正在存儲一個指向局部變量的指針 - 這最多是未定義的行爲。

你爲什麼不做出EdgeList都正常向量,並直接在createEdgeList

+0

謝謝我的困惑來自於使用Xcode來構建我的程序它不喜歡你所建議的方式,但在終端中編譯它工作得很好 – danielBB