我有一个递归函数,它返回一个结构列表。
struct Neighbour_node{
int index;
double dist;
};
这是功能:
list<Neighbour_node> findNewNeighbours(int original, int particle, int k){
Neighbour_node node;
list<Neighbour_node> neighbours;
list<Neighbour_node> temp_neighbours;
list<Neighbour_node>::iterator iterator;
if (k <= 0){
if (particle == -1){
node.index = -1;
node.dist = 1000.0;
}
else{
node.index = particle;
node.dist = glm::length(hair[original].position - hair[particle].position);
neighbours.push_back(node);
}
}
else {
for (unsigned int i = 0; i < hair[particle].neighbours.size(); i++){
temp_neighbours = findNewNeighbours(original,hair[particle].neighbours[i],k - 1);
temp_neighbours.sort(compareNeighbour_node);
neighbours.merge(temp_neighbours,compareNeighbour_node);
}
}
return neighbours;
}
线:
temp_neighbours = findNewNeighbours(original,hair[particle].neighbours[i],k - 1);
导致分段错误,我不知道为什么。我已经看到了与我的错误行相似的示例,看来这并没有错。但是这些函数不是递归的,所以我猜这就是问题所在 - 此外,当 k = 0 时(只有一次函数调用 - 因此好像它不是递归的),那么它不会崩溃。任何人都可以帮我解决这个问题吗?谢谢