0

Hi I know there are a lot of similar questions but I've been through them and I can't seem to make my function work. I need to return a pointer to a 2D array. So far I am using this code:

(This code is a function in Level.cpp)

TileType* CLevel::getTiles()
{
TileType (*matrix_ptr)[31] = tiles;

return *matrix_ptr;
 } 

(TileType is an enum) This function is just returning one row and I obviously need both. Any suggestions?

Header file Level.h:

class CLevel 
{
private:

list<CBox> boxes;
TileType tiles[GRID_HEIGHT][GRID_WIDTH];
CPlayer player;

public:
CLevel();
~CLevel();

CPlayer* getPlayer();
list<CBox>* getBoxes();
TileType** getTiles();
};
2
  • tiles is defined like so: TileType tiles[GRID_HEIGHT][GRID_WIDTH]; Commented May 19, 2012 at 10:56
  • tiles is defined in the header file. I'll edit my question to show the header file Commented May 19, 2012 at 11:13

3 Answers 3

2

Don't define getTiles().

You are completely breaking the encapsulation of the class. This doesn't always matter but in this case the C/C++ 2D array is not a fit structure for passing outside where its dimensions might not be known.

So define your operations on tiles as methods of CLevel.

Sign up to request clarification or add additional context in comments.

1 Comment

However flawed the design might be, this is irrelevant to the question asked
1

What you should do is either this:

// Class declaration
class CLevel
{
public:
   TileType (*getTiles())[GRID_WIDTH];

   TileType tiles[GRID_HEIGHT][GRID_WIDTH];

   //...
};

// Implementation
TileType (*CLevel::getTiles())[GRID_WIDTH]
{
   return tiles;
}

or this:

// Class declaration
class CLevel
{
public:
   TileType (&getTiles())[GRID_WIDTH][GRID_HEIGHT];

   TileType tiles[GRID_HEIGHT][GRID_WIDTH];

   //...
};

// Implementation
TileType (&CLevel::getTiles())[GRID_WIDTH][GRID_HEIGHT]
{
   return tiles;
}

It's a bit of a complicated declaration but read it inside out: in both cases getTiles() is a function that returns a reference to a 2D array of tiles (the example shows two forms of syntax). By calling getTiles() you're actually referring to tiles. You can even call the function it in this way: getTiles()[i][j].

11 Comments

It works, but I don't see many places where such a construct would be useful.
I have used the function you wrote here and I am getting an error that 'tiles' is undefined. I cannot understand how I am getting this error :/ I am also getting an error that 'function returning array is not allowed'. Also I need to access the elements of the array ultimately so I need to call the function as you wrote: getTiles()[i][j]. (I am using this function to store tiles in an array for a Sokoban game)
Well it compiles for me on gcc-4.3.4, see this. What compiler are you using?
I am using Visual C++ 2010 Express. You are not making the function part of a class though. My function looks like this: TileType CLevel::(*getTiles())[GRID_WIDTH] { return tiles; }
Of course, it is still valid. I've just modified the answer to show you two ways of declaring getTiles. If you find this answer helpful, please upvote it and then accept it according to here.
|
-1

If you want to return a pointer to a 2d array, then your function declaration should be:

TileType** CLevel::getTiles()

And your return should be matrix_ptr, not its pointer content (which is a one-dimension array).

return matrix_ptr;

7 Comments

When I do this I get an error: Return value type does not match the function type
So, how is your matrix_ptr declaration?
@RicardPérezdelCampo matrix_ptr is defined within getTiles.
Like I wrote above: TileType (*matrix_ptr)[31] = tiles;
A 2D array cannot be converted to a **.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.