[英]Xcode: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) when making adjacency list
I'm writing a program that reads a file consisting of courses and the pre-reqs for each course. 我正在编写一个程序,该程序读取包含课程和每个课程的先决条件的文件。 I'm supposed to print an ordering in which you could take all of the listed courses so that you don't take any courses before you've taken all the required pre-req courses.
我应该打印一份订购单,您可以在其中订购所有列出的课程,以便在上完所有必修的必修课之前不要上任何课。 To do this I made an adjacency matrix that uses an array of linked lists to store the vertices.
为此,我制作了一个邻接矩阵,该矩阵使用链接列表的数组存储顶点。 Whenver I run it I get the Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) error.
每当我运行它时,我都会收到线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)错误。 It happens sometime after it outputs the number of vertices.
它在输出顶点数量后的某个时间发生。 I've been playing around with the breakpoints for a while but I havn't seen any progress.
我已经在断点上玩了一段时间了,但是我没有看到任何进展。 Can anyone point me in the right direction?
谁能指出我正确的方向? Full Code:
完整代码:
Header: 标头:
#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 */
Main: 主要:
#include "adjList3.h"
int main() {
string filename;
cout << "What is the filename? " << endl;
cin >> filename;
adjList<string> G(filename);
}
The problem is here: 问题在这里:
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;
}
}
You get the first vertex's name and you create a vertex
object to store it. 您将获得第一个顶点的名称,并创建一个
vertex
对象来存储它。 You then get the next vertex's name, but you never actually create the vertex
object to store it. 然后,您将获得下一个顶点的名称,但是您实际上从未创建
vertex
对象来存储它。 Then you search for it, but there's no vertex
there. 然后您搜索它,但是那里没有
vertex
。 Furthermore, in addDirectedEdge()
, you're assuming the size of the list is numV
, but you haven't actually read in numV
vertexes yet. 此外,在
addDirectedEdge()
,您假定列表的大小为numV
,但实际上尚未读取numV
顶点。
I would make sure to create and add each vertex
to the list as you read them in. 在阅读它们时,请确保创建每个
vertex
并将其添加到列表中。
So the constructor for adjList()
might look like this: 因此
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.