2015-04-13 91 views
-3

我试图使用递归函数在有向图上应用连接图算法。递归函数在Graph类中,被称为StrongDFS()。当程序到达for循环崩溃,并给了我/ *错误信息:以std :: out_of_range类型的未捕获异常终止:向量C++

"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 
    } 
+1

请将其缩小。如果你错过了任何事实,你需要自己做调试,然后找到关于编程语言的问题。干杯 –

回答

1

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

您构建一个Vertex对象,使成员处于未初始化状态。这个问题随后出现这样的台词:

verticies[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) {} 
//... 
}; 

你也做同样的事情与你Graph对象。您创建一个包含未初始化成员的图。该Graph应该有一个默认的构造函数:

Graph() : count(0), temp(0), c2(0), w(0) {} 

如果没有这一点,count是未初始化的,和你的StrongDFS功能开始了全乱了这里:

count++; 

由于count是未初始化的,你不知道什么count将是。

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

现在,无论这些变化是否最终解决了您的问题,我都不知道。我所知道的是,你必须做出这些改变,因为在初始值之前明显使用了这些变量。

相关问题