【问题标题】:passing vector container to other functions将向量容器传递给其他函数
【发布时间】:2012-11-27 03:32:45
【问题描述】:

这个问题是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 时,使用: 边缘列表->空; 该函数不应该是空的。 每次我需要使用 createEdgeList 函数时都会调用它,因此它不为空。 如何将数据从我的私有变量传递给函数?

【问题讨论】:

  • stackoverflow.com/questions/13783090/… 链接没有添加抱歉
  • 为什么edgeList是一个裸指针?
  • 如果大部分代码甚至没有远程连接到您的实际问题,您为什么觉得有必要让我们涉足这么多代码?

标签: c++ list graph vector nodes


【解决方案1】:
vector<list<Node> > myEdgeList;    
...
edgeList = &myEdgeList;

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

为什么不把edgelist做成一个法线向量,直接在createEdgeList中加进去

【讨论】:

  • 感谢我的困惑来自使用 Xcode 构建我的程序,它不喜欢你建议的方式,但在终端中编译它工作得很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 2012-03-07
  • 2015-08-07
  • 1970-01-01
  • 2011-12-02
  • 2013-09-02
  • 1970-01-01
相关资源
最近更新 更多