Sign in to follow this  
Ekim_Gram

Problem with struct constructors and initialization

Recommended Posts

Alright, well after having a programming revelation during History class today, I finally figured out how to go about storing the game pieces and such. I'm going to use a linked list of structs to represent each piece and it's positions. Here's my code so far:
#include <iostream>
using namespace std;

// This struct will be added to a linked list for each type of block.
// Thinking of creating a different Block struct for the positions for the different
// pieces. Then they will be added to the linked list.
typedef struct strBlock *BlockPtr_s;
typedef struct strBlock
{
	// Constructor that takes a 4x4 array of bools to initialize the Grid
	strBlock(bool Blocks[4][4])
	{
		for (int i=0; i<4; i++)
			for (int j=0; j<4; j++)
				Grid[i][j] = Blocks[i][j];
	}

	// A 4x4 grid, identifying how the block will be drawn. A cell with the value of 1
	// will be drawn.
	bool Grid[4][4];

	// This is where the linked list comes in. This will hold the positions of the bricks
	// in the block when it undergoes a rotation to the right
	BlockPtr_s RightRotation;

	// ^^^ Likewise
	BlockPtr_s LeftRotation;
} Block_s;

int main()
{
	bool Grid[4][4] = {0, 0, 0, 0,
		 	   0, 0, 1, 1,
			   0, 1, 1, 0,
			   0, 0, 0, 0};
	bool Grid2[4][4] = {0, 0, 1, 0,
			    0, 0, 1, 1,
			    0, 0, 0, 1,
			    0, 0, 0, 0};

	BlockPtr_s Square(Grid);
	BlockPtr_s Square2(Grid2);

	Square->RightRotation = Square2;
	Square2->LeftRotation = Square;

	for (int i=0; i<4; i++)
	{
		for (int j=0; j<4; j++)
			cout << Square->Grid[i][j];
		cout << "\n";
	}
	cout << "\n";

	for (int i=0; i<4; i++)
	{
		for (int j=0; j<4; j++)
			cout << Square2->Grid[i][j];
		cout << "\n";
	}

	return 0;
}



Now, my problem is this - I get errors when padding Grid and Grid2 as parameters for the constructor. And here they are:
Quote:
main.cpp(61): error C2440: 'initializing' : cannot convert from 'bool [4][4]' to 'BlockPtr_s' main.cpp(62): error C2440: 'initializing' : cannot convert from 'bool [4][4]' to 'BlockPtr_s'
It workes fine when I'm not using pointers but it's kind of necessary to use pointers when your aiming for performance in a game. I tired typecasting but unfortunately, that didn't work. Any takers on this fine pain in the ass that I'm unable to solve myself? :) [/source]

Share this post


Link to post
Share on other sites


BlockPtr_s Square(Grid);
BlockPtr_s Square2(Grid2);

//expands out to
struct strBlock * Square(Grid);
struct strBlock * Square2(Grid2);



Which is the problem. You are using pointers that have not been initialized, that is they are "dangling" into invalid memory.

Either change the definitions to strBlock (so they get allocated on the stack) or use 'new' to allocate memory for them on the heap.


BlockPtr_s Square = new strBlock(Grid);
BlockPtr_s Square2 = new strBlock(Grid2);



If you chose this way dont forget to delete them later.

Alan

Share this post


Link to post
Share on other sites
I have no idea what you're trying to achieve with the lines:

BlockPtr_s Square(Grid);
BlockPtr_s Square2(Grid2);

If you didn't add the descriptive text after your code, I would have guessed that you were trying to declare some Block_s objects, and just wrote BlockPtr_s by accident. Just what are you expecting the pointers to do?

Share this post


Link to post
Share on other sites
Wow, I should really re-learn pointers. Haha, I had this same exact problem when I was going over a tutorial from an article series here. You would have thought I'd have learned by now. Thanks a bunch!

Share this post


Link to post
Share on other sites
You are using C++, so don't do this "typedef struct" nonsense. It is a workaround for C code so that you wouldn't have to type "struct" everywhere.


struct Block {
// or hell, "class strBlock", even...
Block(bool Blocks[4][4]) {
// etc.
}

bool Grid[4][4];
Block* RightRotation;
Block* LeftRotation;
};

int main() {
// initialize grids...

Block Square(Grid);
Block Square2(Grid2);

// As noted, you want to create actual Blocks rather
// than Block*'s if you're not using "new". However,
// you will need to get pointers to the Blocks in order
// to link them together, thus the & signs.

Square->RightRotation = &Square2;
Square2->LeftRotation = &Square;

// Print debugging stuff... be sure to use Square.Grid
// etc. rather than Square->Grid, since you have values
// instead of pointers.

// Alternatively, make the things on the heap:
// Block * Square = new Block(Grid);
// But be sure that you know what you're doing with
// all the heap management stuff first!

return 0;
}



Oh, and you *are* going to be using arrays (or std::vectors) later to store those blocks instead of a whole bunch of similarly named variables right? [smile] (19 by my count, assuming you're doing what I think you're doing...)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this