方法调用时出现分段错误
我在尝试调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说你的“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.