【问题标题】:Can't resolve Segmentation Fault core dump error无法解决分段错误核心转储错误
【发布时间】:2014-04-26 23:46:11
【问题描述】:

我正在编写一个为图形实现 Dijkstra 算法的程序,我遇到了这个问题: 分段错误(核心转储)。我已经缩小了导致错误的行(我已经标记了发生错误的行),然后我在我的程序中同时使用了 GDB 和 Valgrind,但所有这些都告诉我错误发生在哪一行,我已经新的东西。你们能给我的任何帮助都会很棒!提前致谢!

    ////HERE'S MY MAIN/////
#include "Vertex.h"
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
void Dijkstra(vector<Vertex> & V);


///overload the less than operator in order to use the stl sort for vector
///print out the path for each vertex

int main()
{

    /////READ ALL THE STUFF INTO THE GRAPH////
    ifstream file;
    file.open("graph.txt");
    cout << "Opening file..." << endl;
    if(!file)
    {
        cout << "System failed to open file.";
    }
    else
    {
        cout << "File successfully opened" << endl;
    }

    int numVertices;
    int numEdges;
    int adjacentVertex;
    int weight;

    file >> numVertices;
    cout << "The number of vertices that are being read into the graph from the file: " << numVertices;
    cout << endl;
    vector<Vertex*> vertices;
    //vector<Vertex> vertices(numVertices + 1);

    for(int i=1;i<=numVertices;i++)
    {   
        file >> numEdges;
        cout << "At vertex " << i << " the number of edges is " << numEdges << endl;
        cout << "Hello" << endl;
        vertices[i] = new Vertex(); ////THIS IS WHERE THE ERROR IS
        cout << "World" << endl;
        //Vertex newVertex;

        //Using the i counter variable in the outer for loop to identify
        //the what vertex what are currently looking at in order to read in the correct adjacent vertex and weight
        vertices[i] -> setVertexNum(i);
        //newVertex.setVertexNum(i);

        for(int j=1;j<=numEdges;j++)
        {
            file >> adjacentVertex;
            cout << "The adjacent vertex is: " << adjacentVertex << endl;


            file >> weight;
            cout << "The weight is: " <<  weight << endl;
            cout << endl;

            vertices[i]->setAdjacentVertex(adjacentVertex, weight);
        }
        vertices.push_back(vertices[i]);
    }
    file.close();


/*
    for(int i=0;i<vertices.size();i++)
    {
        cout << "V" << i <<":  ";
        cout << endl;
        for(int j=0;j<vertices[i].getAdjacentVertices().size();j++)
        {
            cout << "V" << vertices[i].getAdjacentVertices()[j].getAdjVertex() << " " << vertices[i].getAdjacentVertices()[j].getWeight() << endl;
        }
    }
*/






    //CALL THE SHORTEST PATH FUNCTION ON THE GRAPH/////



}


    ////HERE'S MY VERTEX CLASS/////
#include "Edge.h"
#include <vector>
#include <climits>
#include <fstream>
using namespace std;
class Vertex
{
    private:
        int vertexNum; //number of the vertex for identification purposes
        int degree;
        bool known;
        vector<Edge> adjacentVertices; //vector of vertices that are adjacent to the vertex we are currently looking at
        int dv; //distance 
        int pv; //previous vertex
        Vertex *vertex;
    public:
        Vertex()
        {
            dv = INT_MAX;
            known = false;
        }

        void setKnown(bool Known)
        {
            known = Known;
        }

        bool getKnown()
        {
            return known;
        }

        void setVertexNum(int VertexNum)
        {
            vertexNum = VertexNum;
        }

        void setDegree(int Degree)
        {
            degree = Degree;
        }

        vector<Edge> & getAdjacentVertices()
        {
            return adjacentVertices;
        }

        int getVertexNum()
        {
            return vertexNum;
        }

        int getDegree()
        {
            return degree;
        }

        int getDV() const 
        {
            return dv;
        }

        void setAdjacentVertex(int AdjacentVertex, int Weight)
        {
            Edge newEdge;
            newEdge.setWeight(Weight);
            newEdge.setAdjVertex(AdjacentVertex);
            adjacentVertices.push_back(newEdge);
        }

        friend ostream & operator <<(ostream & outstream, Vertex & vertex)
        {
            outstream << vertex.vertexNum << endl;
            outstream << vertex.degree << endl;
            outstream << vertex.known << endl;
            vector<Edge> E = vertex.getAdjacentVertices();
            for(int x=0;x<E.size();x++)
            {
                outstream << E[x].getAdjVertex() << endl;
                outstream << E[x].getWeight() << endl;
            }
            return outstream;
        }

        friend bool operator < (const Vertex & v1, const Vertex & v2);

        void printVertex()
        {

        }


};

【问题讨论】:

    标签: c++ segmentation-fault


    【解决方案1】:

    在初始化vector 的元素时,您应该使用push_back 方法。

    所以改变:

    vertices[i] = new Vertex();
    

    到:

    vertices.push_back( new Vertex() );
    

    当然,您的代码中还有其他问题。我可以给您的一个提示是迭代您的 for 循环,以 0 开头并在谓词中使用 &lt; 而不是 &lt;=

    【讨论】:

      【解决方案2】:

      所以你必须换行

      vector <Vertex*> vertices;
      

      vector<Vertex*> vertices(numVertices);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-08
        • 1970-01-01
        • 2017-04-05
        • 1970-01-01
        • 2016-03-13
        • 2014-08-04
        • 2012-11-19
        相关资源
        最近更新 更多