c++ - Xcode: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) 制作邻接表时
问题描述
我正在编写一个程序,它读取一个由课程和每门课程的先决条件组成的文件。我应该打印一个订单,您可以在其中参加所有列出的课程,这样您就可以在参加所有必需的先决条件课程之前不参加任何课程。为此,我制作了一个邻接矩阵,它使用链表数组来存储顶点。每当我运行它时,我都会收到 Thread 1: EXC_BAD_ACCESS (code=1, address=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
您阅读它们时创建并添加到列表中。
因此 for 的构造函数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();
}
推荐阅读
- ios - 加载预加载到 Core Data 中的 SQLite 数据库
- unit-testing - BDD Mockito - 使用参数捕获器时验证(...)的别名?
- css - 在Flask中使用子域时,如何访问css url中引用的静态文件和使用require.js的js文件?
- angular - Angular6 ngrx:无法读取未定义的属性“ID”
- python - 我想在熊猫中对列进行分组
- python - Pandas + sqlalchemy:通过 pandas 将多个特定列从 csv 提取到 sqlite
- html - ASP.NET Web 应用程序 - Web 表单 - HTML 问题
- angular - Angular:它是否与 ReactJS 的“和解”等效?
- sql - SQL多行计算
- angularjs - AngularJS 'SCRIPT1002: Syntax error' IE9+ 尝试 .filter