我在尝试调用addEdge(int, int)
方法时遇到分段错误。 calling code
在下方。有人可以帮忙吗?
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);
}
任何想法?
答案 0 :(得分:1)
我会说你的'g'对象没有正确实例化。使用'new'创建并使用 - &gt;运营商调用addEdge函数。