[英]Xcode: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) when making adjacency list
我正在编写一个程序,该程序读取包含课程和每个课程的先决条件的文件。 我应该打印一份订购单,您可以在其中订购所有列出的课程,以便在上完所有必修的必修课之前不要上任何课。 为此,我制作了一个邻接矩阵,该矩阵使用链接列表的数组存储顶点。 每当我运行它时,我都会收到线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)错误。 它在输出顶点数量后的某个时间发生。 我已经在断点上玩了一段时间了,但是我没有看到任何进展。 谁能指出我正确的方向? 完整代码:
标头:
#ifndef adjList3_h
#define adjList3_h
//#include <vector>
#include <string>
#include <iostream>
//#include <queue>
#include <fstream>
using namespace std;
template <class T>
class adjList
{
private:
// class neighbor{
// public:
// string name;
// neighbor * next;
// bool mark;
// //constructor
// neighbor(T x)
// {
// name = x;
// next = NULL;
// mark = false;
// }
// };
class vertex
{
public:
T name;
vertex * next;
bool mark;
vertex(T x)
{ name = x;
next = NULL;
mark = false;
}
};
vertex ** arr; //array of vertex objects, collection of linked lists
int numV;//number of vertices
vertex * findVertex(string x, int size, vertex ** arr)
{
for (int i = 0; i < size; i++)
{
if (arr[i]->name == x)
return arr[i];
}
return NULL;
}
// neighbor * findNeighbor(string x, int size, vertex ** arr)
// {
// for (int i = 0; i < size; i++)
// {
// if(arr[i]->name == x)
// {
// return arr[i];
// }
// }
// return NULL;
// }
public:
adjList(string fileName)
{
string adjacentVertex, firstVertex;
ifstream inFile;
inFile.open(fileName);
if(!inFile)
{
cout << "Error opening file..." << endl;
}
inFile >> numV;
arr = new vertex*[numV]; //create an array of vertices
cout << "Number of vertices: " << numV << endl;
for (int i = 0; i < numV; i++)
{
inFile >> firstVertex; //read the source vertex name
arr[i] = new vertex(firstVertex); //add a vertex with the source name to the graph
inFile >> adjacentVertex; //read the next adjacent's name
//while the adjacent name isn't the -999 sentinel
//add an edge from source->destination
//read the next adjacent name
while (adjacentVertex != "\n")
{
//add directed edge from source vertex to neihgbors (class to pre-reqs)
addDirectedEdge(firstVertex, adjacentVertex);
inFile >> adjacentVertex;
}
}
delete [] arr;
inFile.close();
}
// bool checkCopy(string name)
// {
// for (int i = 0; i < numV; i++)
// {
// if(arr[i]->name == name)
// {
// return true;
// }
// }
// return false;
// }
//
//add a directed edge going from x to y (class to pre-reqs)
void addDirectedEdge(T x, T y)
{
//we want to add a directed edge from the vertex to neighbors
vertex * source = findVertex(x, numV, arr);
vertex * destination = findVertex(y, numV, arr);
if (source != NULL && destination != NULL)
{
source->next = destination;
}
}
};
#endif /* adjList3_h */
主要:
#include "adjList3.h"
int main() {
string filename;
cout << "What is the filename? " << endl;
cin >> filename;
adjList<string> G(filename);
}
问题在这里:
for (int i = 0; i < numV; i++)
{
inFile >> firstVertex; //read the source vertex name
arr[i] = new vertex(firstVertex); //add a vertex with the source name to the graph
inFile >> adjacentVertex; //read the next adjacent's name
//while the adjacent name isn't the -999 sentinel
//add an edge from source->destination
//read the next adjacent name
while (adjacentVertex != "\n")
{
//add directed edge from source vertex to neihgbors (class to pre-reqs)
addDirectedEdge(firstVertex, adjacentVertex);
inFile >> adjacentVertex;
}
}
您将获得第一个顶点的名称,并创建一个vertex
对象来存储它。 然后,您将获得下一个顶点的名称,但是您实际上从未创建vertex
对象来存储它。 然后您搜索它,但是那里没有vertex
。 此外,在addDirectedEdge()
,您假定列表的大小为numV
,但实际上尚未读取numV
顶点。
在阅读它们时,请确保创建每个vertex
并将其添加到列表中。
因此adjList()
的构造函数可能如下所示:
adjList(string fileName)
{
string adjacentVertex, firstVertex;
ifstream inFile;
inFile.open(fileName);
if(!inFile)
{
cout << "Error opening file..." << endl;
}
inFile >> numV;
arr = new vertex*[numV]; //create an array of vertices
cout << "Number of vertices: " << numV << endl;
for (int i = 0; i < numV; i++)
{
inFile >> firstVertex; //read the source vertex name
arr[i] = new vertex(firstVertex); //add a vertex with the source name to the graph
inFile >> adjacentVertex; //read the next adjacent's name
//while the adjacent name isn't the -999 sentinel
//add an edge from source->destination
//read the next adjacent name
vertex* prevVertex = arr[i];
while (adjacentVertex != "\n")
{
vertex* nextVertex = new vertex(adjacentVertex);
//add directed edge from source vertex to neihgbors (class to pre-reqs)
prevVertex->next = nextVertex;
prevVertex = nextVertex;
inFile >> adjacentVertex;
}
}
delete [] arr;
inFile.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.