2015-04-13 92 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將是。

擁有未初始化的成員不是很好的做法,特別是如果您稍後要使用這些成員。

現在,無論這些變化是否最終解決了您的問題,我都不知道。我所知道的是,你必須做出這些改變,因爲在初始值之前明顯使用了這些變量。

相關問題