无法弄清楚为什么代码行抛出异常

时间:2013-12-21 08:40:45

标签: c++ oop exception segmentation-fault pass-by-reference

class Graph //part of graph.h, functions go in graph.cpp (functions not shown)
{
private:
    vector< list<Edge> > adjList; //Vector of Lists (sort of like a 2D array)
public:
    Graph(){}
    ~Graph(){}
    class Edge
    {
    public:
        Edge(string vertex, int weight)
        {
            m_vertex = vertex;
            m_weight = weight;
        }
        ~Edge(){}
        string m_vertex;
        int m_weight;
    };

    vector < list < Edge > >& get_adjList(){return adjList;}
    //Other functions....

}; 

我试图在另一个名为MinPriority.cpp的文件中访问adjList数据结构(这些名称并不重要)。整个程序在graph.cpp中运行(我可以构建结构很好并在graph.cpp中打印)但我似乎无法使用下面的代码从另一个文件(使用完全不同的类)访问adjList :

void MinPriority::referenceVector()
{
    Graph graph;
    vector< list<Graph::Edge> >& adjList = graph.get_adjList();
    cout << "adjList test: " << adjList[0].front().m_vertex << endl;
}

为简单起见,我们只需将元素Edge("A", 0)添加到vector< list<Edge> > adjList;中,这将使列表向量包含adjList[0].front().m_vertex处的字符串A和adjList[0].front().m_weight处的0。然后我将从文件graph.cpp打印它,它会告诉我有1个Edge包含权重0和顶点A.(好!它应该这样做!)

一旦我点击MinPriority::referenceVector(),真正的问题就从void vector< list<Graph::Edge> >& adjList = graph.get_adjList();开始了。终端告诉我:

A
ADDING A TO VECTOR
(A, 0) -->
Exception: STATUS_ACCESS_VIOLATION at eip=0043CE11
eax=00000000 ebx=00000000 ecx=00000000 edx=00000000 esi=61276EC0 edi=611A1E9B
ebp=0028AA68 esp=0028AA50 program=C:\cygwin\home\Ryan\311\P5Dec16\Graph.exe, pid 6188, thread main
cs=0023 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame     Function  Args
0028AA68  0043CE11  (00000000, 00000000, 0028AB08, 004026E3)
0028AA78  0043CEAD  (00000000, 00000000, 0028ABB0, 0028AB7C)
0028AB08  004026E3  (0028AC20, 0028ABB0, 20010100, 004011C1)
0028AC68  00401583  (00000001, 0028AC90, 20010100, 612757A2)
0028ACF8  6100763A  (00000000, 0028CD78, 61006C50, 00000000)
End of stack trace
Segmentation fault (core dumped)

这引出了我的问题,vector< list<Graph::Edge> >& adjList = graph.get_adjList();出了什么问题,我该如何解决?注意:我试图包含尽可能多的信息,如果您需要其他任何内容,请发表评论。

编辑:我再次在GDB中运行它,这次由于某种原因它给了我更多信息!生病发布在下面!

IN MAIN
A
ADDING A TO VECTOR
(A, 0) -->

Program received signal SIGSEGV, Segmentation fault.
0x0043ce11 in std::list >::begin (
    this=0x0)
    at /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_list.h:600
600           { return this->_M_impl._M_node._M_next; }
(gdb) where
#0  0x0043ce11 in std::list >::begin (
    this=0x0)
    at /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_list.h:600
#1  0x0043cead in std::list >::front (
    this=0x0)
    at /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_list.h:716
#2  0x004026e3 in MinPriority::createArray (this=0x28abe0,
    targetVertex=0x28ab70) at MinPriority.cpp:59
#3  0x00401583 in main () at MSTapp.cpp:39
(gdb)

1 个答案:

答案 0 :(得分:3)

只要你的构造函数没有自动为你的邻接列表添加边,它就会变空。因此,您尝试访问空图表上的边缘:

void MinPriority::referenceVector()
{
    Graph graph;  // << empty graph
    vector< list<Graph::Edge> >& adjList = graph.get_adjList(); // << empty list

    //   accessing an empty list       vvv      will result in access violation
    cout << "adjList test: " << adjList[0].front().m_vertex << endl;
}

您需要在访问其列表之前填写图表。此外,在实际访问之前,检查给定列表/向量是否包含要访问的元素。更有可能的是,您希望在图表上操作,因此类似

void MinPriority::referenceVector(Graph & graph)
{
    vector< list<Graph::Edge> >& adjList = graph.get_adjList();

    /* ... */
}

可能是你实际想到的。