Hi I have a class MapMaker

class MapMaker {
private:
	Node *grid[MAP_WIDTH][MAP_HEIGHT];
public:
	MapMaker();
	~MapMaker();
	/*this does not work*/ Node*** getMap(int i);


};

I need a function that returns the "grid" attribute but I do not know how. Please help me. Below is the code that implements the logic for my function.

/*what to return here*/ MapMaker::getMap(int i) {
	for (int i = 0; i < MAP_HEIGHT; i++) {
		for (int j = 0; j < MAP_WIDTH; j++) {
			grid[j][i] = new Node(j, i);
		}
	}

	return grid;
}

Thanks!

I tried a bunch of things but none worked out.... the compiler seems to want a Node* (*)[MAP_HEIGHT] but putting that in explicitly didn't do it

How about dynamically allocating your Node, then your return type
can be of Node***.

Alternative, you can use 3d vectors and have the return types as 3d
vectors as well.

So is there no other way but to follow firstPerson's suggestion? I just want to have a 2d array of Node pointers so that inside the class where I need to use this, I can just access it like an ordinary 2d array.

Node *node_from_grid = grid[x][y];

There are other alternatives like boost shared_ptr where
more than 1 pointer can have reference to an object.

I assume Node is a class or a struct, so have the user pass in
a Node *[MAP_WIDTH][MAP_HEIGHT] and from there you can copy the
original content onto the array that passed on, note what you are trying
to do might be expensive depending on the size of you 3d array.

Hi, Node is a class I made. I think I will use the

Node*** grid;

But I have problems initializing it. What is the correct syntax for initializing this so that I can access it like a 2d array? For example, I want to have a method like this:

void myMethod(Node*** grid) {
   //calls the toString() method of Node[0][3]
   cout << grid[0][3]->toString() << endl;
}

Thanks!

Hven't tried it out and its about 3 am but I'll give it a shot :

I will build up :

CASE A : First to create a 1d array :

const int NODE_SIZE = 5;
Node * p = new Node[NODE_SIZE];

CASE B : Now to create a 2 d array you need to have each of the node
element carry some more node element.

const int NODE_X = 5;
const int NODE_Y = 5;
Node **p = new Node*[NODE_X];

//each node_x needs its own node y to create a 5x5 2d array
for(int i = 0; i < NODE_X; i++){
        p[i] = new Node[NODE_Y]

CASE C: //For a 3d array each node has to has NODE_Y element and each
NODE_Y element has to have its NODE_Z element

const int NODE_X = 5;
	const int NODE_Y = 5;
	const int NODE_Z = 5;
	
	//currentl 1d array
	Node*** p = new Node**[NODE_X];
	//now a 2d array
	for(int i = 0; i < NODE_X; i++){
		p[i] = new Node*[NODE_Y];		
	}
	//now a 3d array
	for(int i = 0; i < NODE_Y; i++)
	{
		for(int j = 0; j < NODE_Y; j++){
			p[i][j] = new Node[NODE_Z];
		}
	}

For case A ( 1 D array ) you can access and write data like this :
Node = ...

For case B (2d array) you can access and write data like this :
Node[j] = ...

For case C( 3d array) you can access and write data like this :
Node[j][k] = ...

In your original post you have :

Node *node_from_grid = grid[x][y];Node *node_from_grid = grid[x][y];

which is case 3 but in the form of : *Node[x][y] which is the same as Node[x][y][0]

To delete the arrays you can do the following for each case, respectively ,

Case A :

delete [] Node;

Case B :

for(int i = 0; i < NODE_X; i++)
     delete Node[i];

Case C :

for(int i = 0; i < NODE_Y; i++)
	{
		for(int j = 0; j < NODE_Y; j++){
			p[i][j] = new Node[NODE_Z];
		}
	}

Thanks firstPerson! Your solution worked perfectly! So this is what I did. Please correct me if the following code has mistakes. My goal is to create a 2d array of Node pointers. So, I declared it as firstPerson suggested.

Node*** grid;

Then I initialize it using this:

//initialize array for the width -> not so sure how this works
grid = new Node**[MAP_WIDTH];

//initialize for the height -> not so sure how this works
for (int i = 0; i < MAP_WIDTH; i++) {
	grid[i] = new Node*[MAP_HEIGHT];
}

//create Node objects
for (int i = 0; i < MAP_HEIGHT; i++) {
	for (int j = 0; j < MAP_WIDTH; j++) {
		grid[j][i] = new Node(j, i);
	}
}

Now, to delete the grid, I used this:

//delete first the Node objects
for (int i = 0; i < MAP_HEIGHT; i++) {
	for (int j = 0; j < MAP_WIDTH; j++) {
		delete grid[j][i];
	}
}

//delete the pointers pointing to the Node objects
for (int i = 0; i < MAP_WIDTH; i++) {
	delete grid[i];
}

//delete the actual array
delete [] grid;

I'm pretty sure that this code has no memory leaks (right?) But I am not sure if my explanation (comments) was correct.

Once again, thank you very much!

This is perfectly right !! Nice, work figuring out the suggestion of FirstPerson .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.