2015-05-13 74 views
-1

我想實現一個使用鄰接表的圖形數據結構。爲了填充,我必須從文件讀取數據。該文件是一個文本文件,第一行包含兩個數字。第一個是頂點數n,第二個是邊數m。在這條線後面會有三行數字的m行。前兩個數字表示無向邊緣的源和目標頂點。第三個數字(正整數)是該邊緣的權重。C++程序崩潰。圖形執行

該文件的內容是這樣的:

5 7 
0 1 3 
0 2 4 
0 3 5 
1 4 10 
2 5 20 
3 4 6 
4 5 4 

但由於某些原因,我至今寫的代碼,使程序崩潰。編譯器沒有提供任何關於原因的提示。 我真的很感謝一些建議。我已經讀了很多關於C++中的指針和引用,但仍然發現它們令人困惑。所以更好地理解它們的好資源真的會有所幫助。

#include <string> 
#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <vector> 

using namespace std; 

struct Vertex 
{ 
    unsigned value; 
    vector<Vertex*> adjList; 
    vector<unsigned> weights; 
}; 

class Graph 
{ 
private: 
    unsigned vertex_count, edge_count; 
    vector<Vertex*> vertices; 


public: 
    Graph(string fileName) 
    { 
     ifstream myFile(fileName); 

     if (myFile.is_open()) 
     { 
      // Processing the first line of the file 
      string aLine; 
      getline(myFile, aLine); 
      stringstream aString(aLine); 

      aString >> vertex_count; 
      aString >> edge_count; 

      // Processing the rest of the file 
      unsigned vert1, vert2, weight; 
      while (getline(myFile, aLine)) 
      { 
       aString= stringstream(aLine); 
       aString >> vert1; 
       aString >> vert2; 
       aString >> weight; 
       addRelation(vert1, vert2, weight); 
      } 
     } 
     else 
      cout << "Unable to open file."; 
    } 

    ~Graph() 
    { 
     for (unsigned i = 0; i < vertices.size(); i++) 
      delete vertices[i]; 
    } 

    void addVertex(unsigned val) 
    { 
     Vertex* newVertex = new Vertex; 
     newVertex->value = val; 
     vertices.push_back(newVertex); 
    } 

    Vertex* findVertex(unsigned val) 
    { 
     for (unsigned i = 0; i < vertices.size(); i++) 
      if (vertices[i]->value = val) 
       return vertices[i]; 
     return nullptr; 
    } 

    void addRelation(unsigned vert1, unsigned vert2, unsigned weight) 
    { 
     Vertex* vertex1 = findVertex(vert1); 
     if (vertex1 == nullptr) { 
      addVertex(vert1); 
      vertex1 = findVertex(vert1); 
     } 

     Vertex* vertex2 = findVertex(vert2); 
     if (vertex2 == nullptr) { 
      addVertex(vert2); 
      vertex2 = findVertex(vert2); 
     } 

     vertex1->adjList.push_back(vertex2); 
     vertex1->weights.push_back(weight); 

     vertex2->adjList.push_back(vertex1); 
     vertex2->weights.push_back(weight); 
    } 
}; 

int main() 
{ 
    Graph myG("graph.txt"); 
    return 0; 
} 
+1

首先,使更多的警告,它總是好的發展時,更高的警告級別。其次,在調試器中運行以查找崩潰。調試器將停在崩潰的位置,讓你檢查(並向上)函數調用堆棧,並讓你檢查每個級別的變量值。當然,您需要使用debug-info構建它才能工作。 –

回答

0

你如果表達不分配比較。

Vertex* findVertex(unsigned val) 
{ 
    for (unsigned i = 0; i < vertices.size(); i++) 
     if (vertices[i]->value = val) // WHOOPS! 
      return vertices[i]; 
    return nullptr; 
} 

更改爲:

Vertex* findVertex(unsigned val) 
{ 
    for (unsigned i = 0; i < vertices.size(); i++) 
     if (vertices[i]->value == val) // FIXED 
      return vertices[i]; 
    return nullptr; 
} 
+0

非常感謝!有效。我正在使用命令行,因此錯過了這個錯誤。我以爲我用指針做錯了什麼。 – romikps

1

你的幾個if語句使用=而不是==。如果啓用編譯器警告,你會發現這樣的:

test.cpp:69:36: warning: using the result of an assignment as a condition without parentheses [-Wparentheses] 
      if (vertices[i]->value = val) 
       ~~~~~~~~~~~~~~~~~~~^~~~~ 
test.cpp:69:36: note: place parentheses around the assignment to silence this warning 
      if (vertices[i]->value = val) 
           ^
       (      ) 
test.cpp:69:36: note: use '==' to turn this assignment into an equality comparison 
      if (vertices[i]->value = val) 
+0

謝謝!它有幫助。這樣一個愚蠢的錯誤:)。我希望我用指針再次搞砸了一些東西。 – romikps