如何复制列表矢量?

时间:2013-12-21 02:43:18

标签: c++ oop data-structures vector copy

在我的程序中,我需要将private数据结构复制(或甚至使用)到完全不同的类的.cpp文件中。目前我甚至只是简单地远程访问它时遇到麻烦,当我尝试打印它时会出现故障。这是我的类的简化版本,其中包含数据结构:

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

};

在一个完全不同的功能中,我尝试这样做......

void MinPriority::testPrint(string targetVertex) //FOR TESTING PURPOSES SO FAR (FAILS TO WORK) SEGMENTATION FAULT NO MATTER WHAT
{

    targetVertex = "A";
    Graph graph;
    graph.adjList = graph.get_adjList(); //adjList is our empty container based on the array of linked lists
    /*1*/cout << graph.get_adjList()[0].front().m_vertex << " TEST!" << endl;

    /*2*/cout << "The very first vertex is: ";
    if(graph.adjList.size() == 0)
        cout << "NULL<!>" << endl;
    else cout << graph.adjList[0].front().m_vertex << endl;
}

注意我将targetVertex设置为'a',因此我的程序编译,因为我的makefile中包含-Werror(分配所需)。当我注释掉/*1*/并向下运行到/*2*/时,输出将无论数据结构中有多少元素,始终为"The very first vertex is: NULL<!>"。在/*1*/我尝试打印出函数get_adjList()返回的对象,但是它读错了:

Exception: STATUS_ACCESS_VIOLATION at eip=611298C5
eax=0A0A0A0A ebx=01010101 ecx=20050884 edx=F5F5F5F5 esi=20058488 edi=20060000
ebp=0028A8D8 esp=0028A8D0 program=C:\cygwin\home\Ryan\311\P5Dec16\Graph.exe, pid 5612, thread main
cs=0023 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame     Function  Args
0028A8D8  611298C5  (20058400, 0000000A, 20058488, 610FD3CA)
0028A938  6115F91F  (0028D41C, 61187720, 0028A958, 0043455D)
0028A988  61137BF7  (0028D41C, 20058400, 00000001, 20058488)
0028A9B8  61137CD5  (20058400, 00000001, 20058488, 61187720)
0028A9D8  610D6745  (00449240, 20058400, 20058488, 004493C4)
0028AA68  004439BA  (004493C0, 6123D5FC, 004452B4, 0028AAA0)
0028AB08  00402756  (0028AC20, 0028ABB0, 20010100, 004011C1)
0028AC68  00401583  (00000001, 0028AC90, 20010100, 612757A2)
0028ACF8  6100763A  (00000000, 0028CD78, 61006C50, 00000000)
End of stack trace
Segmentation fault (core dumped)

基本上简单来说,我想知道这个堆栈跟踪是什么(之前我有过seg故障,但我从未见过这个)。我想知道如何在#include "Graph.h"的其他文件中正确访问类Graph中的数据结构。我也不确定如何在testPrint();内复制我的对象。为什么这在Graph.cpp中完美无缺?

void Graph::set_array(string vertex)
{
    //increment vector size by 1 and insert a new Edge object into the vector of linked lists
    cout << "ADDING " << vertex << " TO VECTOR" << endl;
    adjList.resize(adjList.size() + 1);
    adjList[adjList.size() - 1].push_back(Edge(vertex, 0));
}

1 个答案:

答案 0 :(得分:1)

Graph graph;

正如您所说,Graph的默认构造函数没有做任何事情。所以在这一点上,graph.adjList是空的。

graph.adjList = graph.get_adjList();

这是一个毫无意义的陈述,它从自身的副本中分配给graph.adjList。由于以前它是空的,现在它仍然是空的。

cout << graph.get_adjList()[0].front().m_vertex << " TEST!" << endl;

这会尝试访问graph.adjList的(副本)的第一个元素。但是graph.adjList是空的(即它没有第一个元素),所以这是未定义的行为。它可能是也可能不是你的段错误的原因,但在进行任何进一步有用的调试之前,它肯定是一个必须修复的问题。