samedi 21 mars 2015

C++ std::vector::push_back Segmentation Fault on a very easy code

So what I'm trying to do is write a program that is a maze. After compiling, I'm getting the following message: Segmentation fault.


I have read tends of postings regarding this but I am still stuck. Do somebody of you know what is wrong with the code and how to avoid it? I would highly appreciate the comment/concern or any kind of opinion regarding it. Any advice or guess would be appreciated.



#ifndef A4_HPP
#define A4_HPP

#include "Maze.hpp"
#include <iostream>
#include <iostream>
#include <vector>
#include <set>


int distance(Maze& maze, int sx, int sy, int fx, int fy) {
std::vector<int> D;

if (sx == fx && sy == fy){
return 0;
}
else {
if (maze.is_open(sx + 1, sy) == true && maze.is_visited(sx + 1, sy) == false){
int Dis1 = 1 + distance(maze, sx + 1, sy, fx, fy);
maze.unmark(sx + 1, sy);
maze.mark(sx + 1, sy);
D.push_back(Dis1);
return distance(maze, sx + 1, sy, fx, fy);

}
if (maze.is_open(sx, sy + 1) == true && maze.is_visited(sx, sy + 1) == false){
int Dis2 = 1 + distance(maze, sx, sy + 1, fx, fy);
maze.unmark(sx, sy + 1);
maze.mark(sx, sy + 1);
D.push_back(Dis2);
return distance(maze, sx, sy + 1, fx, fy);

}
if (maze.is_open(sx - 1, sy) == true && maze.is_visited(sx - 1, sy) == false){
int Dis3 = 1 + distance(maze, sx - 1, sy, fx, fy);
maze.unmark(sx - 1, sy);
maze.mark(sx - 1, sy);
D.push_back(Dis3);
return distance(maze, sx - 1, sy, fx, fy);

}
if (maze.is_open(sx, sy - 1) == true && maze.is_visited(sx, sy - 1) == false){
int Dis4 = 1 + distance(maze, sx, sy - 1, fx, fy);
maze.unmark(sx, sy - 1);
maze.mark(sx, sy - 1);
D.push_back(Dis4);
return distance(maze, sx, sy - 1, fx, fy);

}

else {
if (maze.is_open(sx, sy) == false){
return 0;
}
}
}
return 0;
}

#endif // A4_HPP

Aucun commentaire:

Enregistrer un commentaire