方法调用时出现分段错误

发布于 2024-11-01 06:36:21 字数 1250 浏览 0 评论 0原文

我在尝试调用 addEdge(int, int) 方法时遇到分段错误。 调用代码如下。有人可以帮忙吗?

    void addEdge(int i, int j) 
{
        if (i >= 0 && j > 0) 
    {
        Node* whereto;
        whereto = linkedAdjacencyList[i];
        if(whereto != NULL) //the segmentation fault occurs here
        {
            while(whereto->adj != NULL)
            {whereto = whereto->adj;}
            whereto->adj = linkedAdjacencyList[j];
        }
        else{linkedAdjacencyList[i]->adj = linkedAdjacencyList[j];}
        whereto = linkedAdjacencyList[j];
        if(whereto != NULL)
        {
            while(whereto->adj != NULL)
            {whereto = whereto->adj;}
            whereto->adj = linkedAdjacencyList[i];
        }
        else{linkedAdjacencyList[j]->adj = linkedAdjacencyList[i];}
            }
    }


std::istream& operator>>(std::istream& in, UndirectedGraph& g)
{
    int numVerticies;
    in >> numVerticies;

    g = UndirectedGraph(numVerticies);
int edges;
in >> edges;
g.edges = edges;
for(int i = 0; i < edges; i++)
{
    int first;
    int second;
    in >> first >> second;
    g.addEdge(first, second);
}

有什么想法吗?

i am getting a segmentation fault while trying to call the addEdge(int, int) method. the calling code is below. Can anyone help?

    void addEdge(int i, int j) 
{
        if (i >= 0 && j > 0) 
    {
        Node* whereto;
        whereto = linkedAdjacencyList[i];
        if(whereto != NULL) //the segmentation fault occurs here
        {
            while(whereto->adj != NULL)
            {whereto = whereto->adj;}
            whereto->adj = linkedAdjacencyList[j];
        }
        else{linkedAdjacencyList[i]->adj = linkedAdjacencyList[j];}
        whereto = linkedAdjacencyList[j];
        if(whereto != NULL)
        {
            while(whereto->adj != NULL)
            {whereto = whereto->adj;}
            whereto->adj = linkedAdjacencyList[i];
        }
        else{linkedAdjacencyList[j]->adj = linkedAdjacencyList[i];}
            }
    }


std::istream& operator>>(std::istream& in, UndirectedGraph& g)
{
    int numVerticies;
    in >> numVerticies;

    g = UndirectedGraph(numVerticies);
int edges;
in >> edges;
g.edges = edges;
for(int i = 0; i < edges; i++)
{
    int first;
    int second;
    in >> first >> second;
    g.addEdge(first, second);
}

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紙鸢 2024-11-08 06:36:21

我想说你的“g”对象没有正确实例化。使用“new”创建它并使用 ->运算符调用 addEdge 函数。

I would say that your 'g' object was not instantiated properly. Create it using 'new' and use -> operator to call addEdge function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文