gpt4 book ai didi

c++ - 无法解决 Segmentation Fault 核心转储错误

转载 作者:行者123 更新时间:2023-11-28 06:54:50 26 4
gpt4 key购买 nike

我正在编写一个为图实现 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()
{

}


};

最佳答案

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

所以改变:

vertices[i] = new Vertex();

到:

vertices.push_back( new Vertex() );

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

关于c++ - 无法解决 Segmentation Fault 核心转储错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23317754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com