Quantcast
Channel: c++ vector with object pointers - Stack Overflow
Viewing all articles
Browse latest Browse all 3

c++ vector with object pointers

$
0
0

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 stackso 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:};

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>