Methods 方法调用中的分段错误
我在尝试调用Methods 方法调用中的分段错误,methods,segmentation-fault,call,Methods,Segmentation Fault,Call,我在尝试调用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
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);
}
void addEdge(int i,int j)
{
如果(i>=0&&j>0)
{
节点*指向何处;
whereto=LinkedAjacyList[i];
if(whereto!=NULL)//此处发生分段错误
{
while(whereto->adj!=NULL)
{whereto=whereto->adj;}
whereto->adj=LinkedAjacyList[j];
}
else{LinkedAjacyList[i]>adj=LinkedAjacyList[j];}
whereto=LinkedAjacyList[j];
if(whereto!=NULL)
{
while(whereto->adj!=NULL)
{whereto=whereto->adj;}
whereto->adj=LinkedAjacyList[i];
}
else{LinkedAjacyList[j]->adj=LinkedAjacyList[i];}
}
}
std::istream&operator>>(std::istream&in,无向图&g)
{
国际货币基金组织;
在>>数字证书中;
g=无向图(NumV垂直度);
int边;
在>>边缘;
g、 边=边;
对于(int i=0;i>第一>>第二;
g、 附录(第一、第二);
}
有什么想法吗?我想说你的“g”对象没有正确实例化。使用“新建”创建它,并使用->运算符调用addEdge函数。我更新了发布的代码以包含更多的背景。当我尝试执行新建和->
时,我遇到了编译错误。addEdge
首先通过和ssecond
作为参数。如果我发送100200呢?程序仍然有效还是容易出现分段错误?@Mahesh是的,用硬编码的整数尝试了它,但仍然在同一个地方出现分段错误:/@JocobL-我只是给出了这些数字作为示例。既然你在方法中对I,j
进行操作,你应该检查在这些位置,无论->
是否会导致有效的内存位置。忘记通过一些输入值。分段错误现在发生在if(whereto!=NULL)
,第一个实例。我在代码中添加了注释以显示准确的位置so,LinkedAjacyList[i];
没有为要传递的i
的值提供有效的内存位置。