Xcode:线程1:exc_bad_access(代码= 1,地址= 0x0)在缩短列表时



我正在编写一个程序,该程序读取一个由课程和每个课程的reqs组成的文件。我应该打印一个订单,您可以参加所有列出的课程,以便您在参加所有必需的req req课程之前不参加任何课程。为此,我制作了一个邻接矩阵,该矩阵使用链接列表数组来存储顶点。当我运行它时,我会得到线程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();
}

相关内容

  • 没有找到相关文章

最新更新