i have to implement a small and simple game in c++ (a maze) and I have some problems right now.
Some snippets: I've got an array of object pointers which represents my fields in the maze
Field*** maze;
init of the maze:
for (n = 0; n < MAZE_WIDTH; n++) {
this->maze[n] = new Field*[MAZE_HEIGHT];
for (p = 0; p < MAZE_HEIGHT; p++) {
this->maze[n][p] = new Field();
this->maze[n][p]->x = n;
this->maze[n][p]->y = p;
}
}
When creating the maze i need a list of already visited fields and a stack so I did:
std::vector<Field*> visited;
std::vector<Field*> stack;
Then later I want to put a Field* into my stack
stack.push_back(neighbour);
But after this push all values in the object are wrong. Even if i try
neighbour = stack.back();
all the values are completly different
I already red some threads about this topic and that's why i chose a vector of pointers and not objects.
Where is my fault?
Edit: Some more snippets as requested:
Of course I allocate memory for the mate itself
this->maze = new Field**[MAZE_WIDTH];
Field is a simple class which looks like:
class Field {
public:
Field();
~Field();
bool w_left;
bool w_right;
bool w_front;
bool w_back;
unsigned int x;
unsigned int y;
private:
};
neighbour, for example? How is it initialized? (It sounds like you might be inserting a pointer to a local variable into thevector, but without more code, it's impossible to tell.)new Field**[]at the beginning ? Also, I think there is simpler ways to handle multidimensional arrays than a pointer to a pointer to a pointer.mazepointer. Another big error is having that pointer in the first place. Folks who do that are commonly to as "three srtar programmers. Use e.g.std::vector. Regarding what it seems that your question is about, namely why things don't work, no real answer appears to be possible given the info you present. It is in the direction of "my program does not Work, here are some unrelated bugs, what are the rest of my bugs?". Difficult to answer, to say the least. Cheers & hth.,***in a C program; let alone in C++.