Jump to content
  • Advertisement
Sign in to follow this  
ISDCaptain01

Cant pass a multidimensional array of objects into a function

This topic is 1888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to make a breakout clone. So I have block class as listed:

class Block
{
public:

	//Variables for the block class
	int x, y;
	int life;
	int rows;
	int columns;
	int width, height;
	BITMAP *sprite;


	//constructor
	Block()
	{
		x = 0;
		y = 0;
		life = 0;
		sprite = NULL;
	}

	//Initialization function
	void initialize(int px, int py, int plife, BITMAP *source)
	{
		//Sets all the data for the blocks according to the player
		x = px;
		y = py;
		life = plife;
		sprite = source;
		columns = 6;
		rows = 3;
	}

	//Sets the dimensions of the blocks
	void setdimensions()
	{
		width = sprite->w;
		height = sprite->h;
	}

	//Draws the blocks on screen
	void drawblocks(BITMAP *dest)
	{
		for(int i = 0; i < rows; i++)
		{
			for(int ii = 0; ii < columns; ii++)
			{
				blit(sprite, dest, 0, 0, 0, 0, width, height);
			}
		}
	}

	//Detects if a collision detection occured or not
	int inside (int x, int y, int left, int top, int right, int bottom)
	{
		if(x > left && y > top && x < right && y < bottom)
			return 1;
		else
			return 0;
	}

	//Checks to see if the star collided with the blocks
	int collided(Star star, int border = 0)
	{
		int width1 = star.x + star.width;
		int height1 = star.y + star.height;

		//see if the corners of the star hit the block
		if(inside(star.x, star.y, x, y, width, height))
		{
			return 1;
		}

		//If no collision detected
		return 0;
	}

};

Okay now I go about making the object in my main file:

Block blocks[3][8];

Everything is good, and I can you a double loop to display the sprites in the class onto the screen. My main problem comes when I pass it into to this function that detects my collision against the star:

 

 


moveStar(starball, paddle1, blocks[3][8]);

Heres the complete function

void moveStar(Star &star, Paddle &paddle, Block block[][8])
{
    //update the star's x position
	star.x += star.dirx;

	//If star hits the left side of the screen
	if(star.x < 0)
	{
		star.x = 1;
		star.dirx = rand() % 2 + 4;
	}

	//If star hits the right side of the screen
	if(star.x > SCREEN_W - star.width - 1)
	{
		star.x = SCREEN_W - star.width - 1;
		star.dirx = rand() % 2 - 4;
	}

	//Update the star's y position
	star.y += star.diry;

	//if star hits the top of the screen
	if(star.y < 0)
	{
		star.y = 1;
		star.diry = rand() % 2 + 4;
	}
	
	//if star hits bottom of the screen
	if(star.y > SCREEN_H - star.width - 1)
	{
		star.y = SCREEN_H - star.width - 2;
		star.diry = rand() % 2 - 4;
	}

	//Check if star hits the paddle
	if(star.x > paddle.x && star.x < paddle.x + paddle.height &&
	   star.y > paddle.y && star.y < paddle.y + paddle.height)
	{
		star.y = paddle.y - star.height - 1;
        star.diry = rand() % 2 - 4;
	}

	//check if star hits the blocks
	for(int y = 0; y < 3; y++)
	{
		for(int x = 0; x < 8; x++)
		{
			if(block[x][y].collided(star) == 1)
			{
				//erase that block
				block[x][y].life = 0;
			}
		}
	}
}

 

When I compile it gives me this error:

error C2664: 'moveStar' : cannot convert parameter 3 from 'Block' to 'Block [][8]'

1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

 

Whats there to convert? Im just passing in a multidimensional array of block objects.

 

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

blocks[3][8] dereferences the array and accesses the 4:th-by-9:th element (which in itself is outside the array), and then passes it to the function, which is the incorrect type as mentioned by the error. If you want to pass the array, then don't dereference any elements of it.

moveStar(starball, paddle1, blocks);

Share this post


Link to post
Share on other sites

void moveStar(Star &star, Paddle &paddle, Block block[][8])

Block block[][8]

That is not a valid type. A 2d array is just an array whose elements are arrays. So an array element should have a size. But what you have here is an array of elements that are of type (Block[]), which does not have a size.

 

You should declare it as Block block[3][8], then the compiler will know that it is an array of elements that are of size (3 * sizeof(Block)).

Share this post


Link to post
Share on other sites

It is a perfectly valid parameter type and is equivalent to Block (*)[8], but it is not a valid variable declaration by itself. But even if you do specify the size, the left-most dimension of the parameter type is irrelevant and carries not meaning, so Blocks block[3][8] is still equivalent to both Blocks block[][8] and Blocks block (*)[8], and you can pass any N-by-8 sized array to any of the three variants.

Share this post


Link to post
Share on other sites

It is a perfectly valid parameter type and is equivalent to Block (*)[8], but it is not a valid variable declaration by itself. But even if you do specify the size, the left-most dimension of the parameter type is irrelevant and carries not meaning, so Blocks block[3][8] is still equivalent to both Blocks block[][8] and Blocks block (*)[8], and you can pass any N-by-8 sized array to any of the three variants.

ah, so does that mean Block[][8] is actually decayed to Block **?

Share this post


Link to post
Share on other sites

You can avoid such messy problems mostly if you think a little more about how to organize your code.

 

For example you could:

- Make a Map class which contains all Blocks and add a method to test for a hit.

- Remove the hit testing and the Block argument from the MoveStar function, because hit testing is not moving.

- Make MoveStar function a method inside Star and rename it to Move.

Share this post


Link to post
Share on other sites

 

It is a perfectly valid parameter type and is equivalent to Block (*)[8], but it is not a valid variable declaration by itself. But even if you do specify the size, the left-most dimension of the parameter type is irrelevant and carries not meaning, so Blocks block[3][8] is still equivalent to both Blocks block[][8] and Blocks block (*)[8], and you can pass any N-by-8 sized array to any of the three variants.

ah, so does that mean Block[][8] is actually decayed to Block **?

 

No, only the left-most dimention decays to a pointer-type. Block[N][8] is equivalent to Block (*)[8] for any N, even if N is left out completely.

Edited by Brother Bob

Share this post


Link to post
Share on other sites

Its probably not "best practices", but when I am doing projects like this I don't actually keep everything self contained I keep a big master class with all of my data members and just pass a reference to my functions that need to access any of my games data.

Share this post


Link to post
Share on other sites

 

 

It is a perfectly valid parameter type and is equivalent to Block (*)[8], but it is not a valid variable declaration by itself. But even if you do specify the size, the left-most dimension of the parameter type is irrelevant and carries not meaning, so Blocks block[3][8] is still equivalent to both Blocks block[][8] and Blocks block (*)[8], and you can pass any N-by-8 sized array to any of the three variants.

ah, so does that mean Block[][8] is actually decayed to Block **?

 

No, only the left-most dimention decays to a pointer-type. Block[N][8] is equivalent to Block (*)[8] for any N, even if N is left out completely.

 

After some small tests, I understand now. Block[3][8] is not an array of length 8 of elements Block[3], it's actually an array of length 3 of elements Block[8].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!