2015-04-03 89 views
0

这是我打电话顶点类,重载< <操作打印 出它的私有成员ID我遇到分段错误,不知道为什么

 case 'v': 

    if (isAlreadyFile == true){ 

     vector <Vertex*> vertices = graph.GetVertices(); 
     cout << "Vertices: " << endl; 

     int currentVerticie = 0; 
     Vertex v = *vertices[currentVerticie]; 

     while (currentVerticie < numVertices){ 

     cout << v << endl; 
     currentVerticie++; 
     v = *vertices[currentVerticie]; 

     } 

    } 

    else{ 

这是Vertex类的实现文件,请帮忙!

#include "Vertex.h" 

using namespace std; 

// Static Member Variables 
int Vertex:: m_IDTotal = -1; 

Vertex::Vertex(){ 

    m_IDTotal++; 
    m_ID = m_IDTotal; 

} 

int Vertex::GetID() const { 

    int ID = m_ID; 
    return ID; 

} 

ostream& operator << (ostream &sout, const Vertex &v){ 

    sout << " ID: " << v.GetID(); 

    return sout; 

} 
+0

引入调试器。在段错误之前,所有变量的值是什么? – 2015-04-03 00:28:58

+0

尝试给出一个小但完整的示例代码,展示您的问题。由于您不知道问题所在,因此挑选一些数据并发布它们可能会排除导致或者成为问题一部分的代码。既然你正在处理一个指针向量,那么向量中指针的设置就是原因 - 你没有看到。 – Peter 2015-04-03 00:29:41

+0

指针的所有向量看起来对我来说都不是最理想的。以及取消引用,而不检查矢量是否大小> 0。 – BitTickler 2015-04-03 01:10:25

回答

0

很可能numVertices的值很差。将其更改为(int)vertices.size()

相关问题