我想訪問另一個名爲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;
,這將使向量的列表中包含字符串A以adjList[0].front().m_vertex
和0的adjList[0].front().m_weight
。然後我將在文件graph.cpp中打印它,它會告訴我有1個包含重量0和頂點A的邊(好!它應該這樣做!)
真正的問題開始在虛空MinPriority::referenceVector()
,只要我打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)
你檢查了'adjList.size()'和'adjList [0] .size()'嗎? – Zeta
我檢查真的很快 – user3040019
它輸出0,所以是的,出於某種原因,它沒有通過引用傳遞任何東西。 – user3040019