我遇到了分段错误,不知道为什么

时间:2015-04-03 00:17:53

标签: segmentation-fault

这是我调用顶点类,重载<<操作员打印 出了它的私人会员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;

}

1 个答案:

答案 0 :(得分:0)

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