C++ 以std::out_of_range:vector c+类型的未捕获异常终止+;

C++ 以std::out_of_range:vector c+类型的未捕获异常终止+;,c++,recursion,vector,C++,Recursion,Vector,我尝试使用递归函数在有向图上应用连通图算法。递归函数位于Graph类中,称为StrongDFS()。当程序到达for循环时,它会崩溃,并给出/*错误消息: "libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector check1check1Run Command: line 1: 72739 Abort trap: 6 ./"$2" "${@:3}""

我尝试使用递归函数在有向图上应用连通图算法。递归函数位于Graph类中,称为
StrongDFS()
。当程序到达for循环时,它会崩溃,并给出/*错误消息:

"libc++abi.dylib: terminating with uncaught exception of type      std::out_of_range: vector
check1check1Run Command: line 1: 72739 Abort trap: 6           ./"$2" "${@:3}""
我不明白为什么在程序中的那个点向量会有问题。任何帮助都将不胜感激

  #include <iostream>
    #include <vector>
     #include <stack>
    using namespace std;


    class Vertex {
      public:
        int currentDist; // needs to be updated 
        int id; // use id as index 
        int pred;
        int num; 
        int in_stack; 
        vector<int> neighbors; // These need to be parallel 


       void setId (const int& x){
          id = x; 
           setNum(0);
           setPred(0);
        }
        void onStack(int os){
            in_stack = os; 
    }
        void addToniegh (const int& n){
          neighbors.push_back(n);
        }

        void setNum(int sn){
        num = sn; 
    }
        void setPred(int p){
        pred = p; 
        }

        friend ostream& operator << (ostream& out, Vertex ver);
    };

    class Graph {   // this class is going to contain the dequeue and print out the graph 
      public:
      vector<Vertex*> verticies; // a list of all verticies 
      int w;// a counter 
      int c2; 
      int count; 
      int temp; 
      stack<int> theStack;   // the numbers wait, if less than the head add to front 
       Vertex vertex; 
      void addVert (Vertex* v){
        verticies.push_back(v);
      }

        int StrongDFS(int sd){
            count++; 
            verticies[sd-1]->setPred(count); 
            verticies[sd-1]->setNum(count); 
            theStack.push(verticies[sd-1]->id);
             cout << "Stack: " << theStack.top() << endl;   
            verticies[sd - 1]->onStack(1);
            cout << verticies[sd-1]->num << "  " << verticies[sd-1]->pred << " " << verticies[sd-1]->neighbors.size() << endl;  
            cout << "hello"<< endl; 
            for (int i = 0; i < verticies[sd - 1]->neighbors.size(); i++){
                   cout << "check1";
                if (verticies[sd - 1]->num == 0){
                    cout << "CHECK";
                    StrongDFS(verticies[sd-1]->neighbors.at(i));
                    if (verticies[sd - 1]->pred > verticies[verticies[sd - 1]->neighbors.at(i) - 1]->pred)
                        verticies[sd - 1]->setPred(verticies[verticies[sd - 1]->neighbors.at(i) - 1]->pred);
                    }   
                else if (verticies[verticies[sd - 1]->neighbors.at(i)]->num < verticies[sd - 1]->num && verticies[verticies[sd]->neighbors.at(i)]->in_stack == 1){
                    if (verticies[sd - 1]->pred > verticies[verticies[sd - 1]->neighbors.at(i) - 1]->num)
                        verticies[sd - 1]->setPred(verticies[verticies[sd - 1]->neighbors.at(i) - 1]->num);
                }}
            if (verticies[sd-1]->pred == verticies[sd - 1]->num){
                w = theStack.top();
                verticies[w - 1]->onStack(0);
                theStack.pop();     
                while (w != sd){
                     cout << "output " << char(w + 'a' - 1) << endl;  
                    w = theStack.top(); 
                    verticies[w - 1]->onStack(0);
                    theStack.pop();
                }
                cout << "output " << char(w + 'a' - 1) << endl;  
            }
            return w; 

            }
            //return NULL; 
      };
    ostream& operator << (ostream& out, Vertex ver) {
      out << char(ver.id+'a'-1) <<  " (" ;
      for (int i = 0; i < ver.neighbors.size(); i++){ 
        if (i != ver.neighbors.size()- 1)
          cout << char(ver.neighbors.at(i)+'a'-1)  << ", ";
        else 
          cout << char(ver.neighbors.at(i)+'a'-1); 
      }
      cout << ")  "<< "current Distance: " << ver.currentDist << endl; 

      return out; 
    }
    int main(int argc, char *argv[]) {
         Vertex v1; //  A
          Vertex v2; // B 
          Vertex v3; // C
          Vertex v4; // D
          Vertex v5; // E
          Vertex v6; // F
          Vertex v7; // G
          Vertex v8; // H 
          Graph g1;

          // - - - - - - - - - - - - A
          v1.setId(1);
          v1.addToniegh(3); // c
          v1.addToniegh(4); // d

          // - - - - - - - -- - - - - B
          v2.setId(2);
          v2.addToniegh(6); // f 
          // - - - - - - - - - - - - - C
          v3.setId(3);
          v3.addToniegh(1); // a
          v3.addToniegh(5);  // e
          // - - - - - - - - - - - - - D
          v4.setId(4);
          v4.addToniegh(2); // b
          v4.addToniegh(5); // e
          // - - - - - - - - - - - - -  E 
          v5.setId(5);
          v5.addToniegh(6); // f
          // - - - - - - - - - - - - - - F
          v6.setId(6);
          v6.addToniegh(7);  // g
          // - - - - - - - - - - - - - - G
          v7.setId(7);
          // - - - - - - - - - - - - - - H
          v8.setId(8);
          v8.addToniegh(6); // f

          //----Adding them to vector in Graph ---
          g1.addVert(&v1);
          g1.addVert(&v2);
          g1.addVert(&v3);
          g1.addVert(&v4);
          g1.addVert(&v5);
          g1.addVert(&v6);
          g1.addVert(&v7);
          g1.addVert(&v8);
          g1.StrongDFS(1);
          //cout << endl;    
          //cout << g1; // I print here 
          //g1.proc();  // I start the graph here
          //cout << g1; // I print here 
    }
#包括
#包括
#包括
使用名称空间std;
类顶点{
公众:
int currentDist;//需要更新
int id;//使用id作为索引
int pred;
int-num;
int in_堆栈;
向量邻居;//这些需要是并行的
void setId(常量int&x){
id=x;
setNum(0);
setPred(0);
}
void onStack(内部操作系统){
in_stack=os;
}
void addToniegh(const int&n){
邻居。推回(n);
}
void setNum(内部序号){
num=sn;
}
void setPred(int p){
pred=p;
}
friend ostream和操作员setPred(计数);
垂直度[sd-1]->setNum(计数);
钉压(垂直[sd-1]->id);
cout.at(i)-1]>pred);
}   
else if(垂直[垂直[sd-1]->相邻.at(i)]->numnum&&Vertices[垂直[sd]->相邻.at(i)]->in_stack==1){
if(垂直[sd-1]->pred>Vertices[Vertices[sd-1]->Neights.at(i)-1]->num)
垂直性[sd-1]->setPred(垂直性[Vertices[sd-1]->邻居.at(i)-1]->num);
}}
if(垂直方向[sd-1]->pred==垂直方向[sd-1]->num){
w=stack.top();
垂直方向[w-1]>onStack(0);
theStack.pop();
while(w!=sd){

我马上看到的一个问题是,您没有为
Vertex
初始化成员变量

您构造了一个
顶点
对象,使成员处于未初始化状态。问题随后出现在如下行中:

vertices[sd-1]->num

当我使用Visual Studio时,我得到了
num
的通配符,这都是由于成员变量未初始化。在输出中,我得到了以下结果:

Stack: 1
-858993459  -858993459 2
您应该编写一个初始化成员变量的默认构造函数:

class Vertex {
public:
    int currentDist; // needs to be updated 
    int id; // use id as index 
    int pred;
    int num;
    int in_stack;
    vector<int> neighbors; // These need to be parallel 
    Vertex() : currentDist(0), id(0), pred(0), num(0), in_stack(0) {}
//...
};
如果没有此选项,
count
将被取消初始化,并且您的
StrongDFS
函数在此处开始时会出现混乱:

    count++;
由于
count
未初始化,您不知道
count
将是什么

拥有未初始化的成员不是很好的做法,特别是如果您以后要使用这些成员


现在,我不知道这些更改是否最终解决了您的问题。我知道的是,您必须进行这些更改,因为在变量具有初始值之前,显然正在使用这些变量。

请缩小范围。您需要自己进行调试,如果您缺少一个初始值,请向我们询问有关编程语言的问题干杯
    count++;