C++ 程序崩溃。图实现

标签 c++ pointers crash

我正在尝试实现一个使用邻接列表的图形数据结构。为了填充,我必须从文件中读取数据。该文件是一个文本文件,其中第一行包含两个数字。第一个是顶点数 n,第二个是边数 m。此行之后将有 m 行包含三个数字。前两个数字表示无向边的源顶点和目标顶点。第三个数字(正整数)是该边的权重。

该文件的内容如下所示:

5 7
0 1 3
0 2 4
0 3 5
1 4 10
2 5 20
3 4 6
4 5 4

但由于某种原因,我到目前为止编写的代码导致程序崩溃。并且编译器没有给出任何关于原因的提示。 我真的很感激一些建议。我读过很多关于 C++ 中的指针、引用的内容,但仍然发现它们令人困惑。因此,一个能够更好地理解它们的好资源确实会很有帮助。

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

struct Vertex
{
    unsigned value;
    vector<Vertex*> adjList;
    vector<unsigned> weights;
};

class Graph
{
private:
    unsigned vertex_count, edge_count;
    vector<Vertex*> vertices;


public:
    Graph(string fileName)
    {
        ifstream myFile(fileName);

        if (myFile.is_open())
        {
            // Processing the first line of the file
            string aLine;
            getline(myFile, aLine);
            stringstream aString(aLine);

            aString >> vertex_count;
            aString >> edge_count;

            // Processing the rest of the file
            unsigned vert1, vert2, weight;
            while (getline(myFile, aLine))
            {
                aString= stringstream(aLine);
                aString >> vert1;
                aString >> vert2;
                aString >> weight; 
                addRelation(vert1, vert2, weight);
            }
        }
        else
            cout << "Unable to open file.";
    }

    ~Graph()
    {
        for (unsigned i = 0; i < vertices.size(); i++)
            delete vertices[i];
    }

    void addVertex(unsigned val)
    {
        Vertex* newVertex = new Vertex;
        newVertex->value = val;
        vertices.push_back(newVertex);
    }

    Vertex* findVertex(unsigned val)
    {
        for (unsigned i = 0; i < vertices.size(); i++)
            if (vertices[i]->value = val)
                return vertices[i];
        return nullptr;
    }

    void addRelation(unsigned vert1, unsigned vert2, unsigned weight)
    {
        Vertex* vertex1 = findVertex(vert1);
        if (vertex1 == nullptr) {
            addVertex(vert1);
            vertex1 = findVertex(vert1);
        }

        Vertex* vertex2 = findVertex(vert2);
        if (vertex2 == nullptr) {
            addVertex(vert2);
            vertex2 = findVertex(vert2);
        }

        vertex1->adjList.push_back(vertex2);
        vertex1->weights.push_back(weight);

        vertex2->adjList.push_back(vertex1);
        vertex2->weights.push_back(weight);
    }
};

int main()
{
    Graph myG("graph.txt");
    return 0;
}

最佳答案

您的一些 if 语句使用 = 而不是 ==。如果您在编译器中启用警告,您会发现类似以下内容:

test.cpp:69:36: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
            if (vertices[i]->value = val)
                ~~~~~~~~~~~~~~~~~~~^~~~~
test.cpp:69:36: note: place parentheses around the assignment to silence this warning
            if (vertices[i]->value = val)
                                   ^
                (                       )
test.cpp:69:36: note: use '==' to turn this assignment into an equality comparison
            if (vertices[i]->value = val)

关于C++ 程序崩溃。图实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30220506/

相关文章:

c - ‘->’ 的无效类型参数

c - 如何使用 LLIST *mylist[N];

c++ - 如何定义指向文件流的静态指针?

c++ - 以位为单位的整数类型大小的元程序

c++ - 有没有办法测试 C++ 类是否具有默认构造函数(编译器提供的类型特征除外)?

c++ - 命名空间 ‘std’ 中的“ostream”未命名类型

java - 致命异常 : java. lang.IllegalStateException - 无法为 LinearLayout 创建图层(仅在 Galaxy j4+、j6+ 中崩溃)

c++ - 可移植代码 - 每个字符的位数

在 ViewPager 中使用 WebViews 的 Android 进程崩溃,硬件加速开启

ios - 崩溃,终止原因 0xdead10cc