Advertisement

Trying to declare an array in a class constructor

Started by August 28, 2001 01:34 AM
14 comments, last by Gwargh 23 years, 5 months ago
Here''s the constructor: Block::Block( UINT getwidth , UINT getheight ) { width = getwidth; height = getheight; bool grid[ width ][ height ]; } And here are the errors I get: error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 error C2087: '''' : missing subscript error C2133: ''grid'' : unknown size I''ve tried making width and height constants, I''ve tried using getwidth and getheight as the subscripts, I''ve tried putting the array declaration into a different function, and nothing has gotten rid of these errors.
"These books suck."-- Zidane
hehe, you cannot declare a static array with variable subscripts.

You have to do something like
bool grid[5][10];
you can use constants too
#define WIDTH 5
#define HEIGHT 10
bool grid[WIDTH][HEIGHT];

if you really want a dynamically allocated array when it is declared, you have to use new and delete, but the trick is... it is not simple! the reason I say it is not simple is because it is a multidimensional array, which means, and array of arrays. Its kinda late, so I''m not going to do it for you right now, but if you have trouble with the multidimensional arrays junk, e-mail me at cpp_kid@hotmail.com and i''ll take a look at it in the morning.

- Tom
Rock on,- Tom
Advertisement
Do this...

class x
{
private:
bool *grid;
}

x::x( int width, int height )
{
grid = new bool[width][height];
}

x::~x()
{
if( grid )
{
delete [] grid;
grid = 0; // NULL
}
}

Then you can access it normally or using pointer arithmetic (I recommend the former).

-Mark

Hm... neither of those worked on its own, but I combined them to make:

Block::Block( UINT getleftsrc , UINT gettopsrc , UINT getwidth , UINT getheight )
{
leftsrc = getleftsrc;
topsrc = gettopsrc;
#define WIDTH getwidth;
#define HEIGHT getheight;
grid = new bool[ WIDTH ][ HEIGHT ];
}

and I put bool *grid in the Block class.

That all worked fine, but now I''m in one of those situations where it seems like the compiler is being a total idiot.

error C2143: syntax error : missing '']'' before '';''
error C2143: syntax error : missing '';'' before '']''
error C2143: syntax error : missing '';'' before '']''

Upon seeing this, I clenched my fists and said, "Compiler, j00 = d3d."

It''s one of those situations...

BTW, I''ve never done stuff quite like this before, so if having a #define statement in a function is dangerous in some way, please let me know.
D''oh, I think something''s wrong with my cookies... that post above was mine.
"These books suck."-- Zidane
quote:
Original post by Anonymous Poster
Hm... neither of those worked on its own, but I combined them to make:

Block::Block( UINT getleftsrc , UINT gettopsrc , UINT getwidth , UINT getheight )
{
leftsrc = getleftsrc;
topsrc = gettopsrc;
#define WIDTH getwidth;
#define HEIGHT getheight;
grid = new bool[ WIDTH ][ HEIGHT ];
}

and I put bool *grid in the Block class.

That all worked fine, but now I''m in one of those situations where it seems like the compiler is being a total idiot.

error C2143: syntax error : missing '']'' before '';''
error C2143: syntax error : missing '';'' before '']''
error C2143: syntax error : missing '';'' before '']''

Upon seeing this, I clenched my fists and said, "Compiler, j00 = d3d."

It''s one of those situations...

BTW, I''ve never done stuff quite like this before, so if having a #define statement in a function is dangerous in some way, please let me know.


You do not need the defines in there. The code you have is exactly the same as...

Block::Block( UINT getleftsrc , UINT gettopsrc , UINT getwidth , UINT getheight )
{
leftsrc = getleftsrc;
topsrc = gettopsrc;
grid = new bool[getwidth][getheight];
}


Steve ''Sly'' Williams  Code Monkey  Krome Studios
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
Advertisement
Wow, I want whatever compiler you guys are using. You can't pass two non-constants directly into a dynamic memory allocation:
    bool **allocate (int x, int y){    bool **array = new bool[x][y];    return array;}  

C:\projects\test\test.cpp(3) : error C2540: non-constant expression as array boundC:\projects\test\test.cpp(3) : error C2440: 'initializing' : cannot convert from 'bool (*)[1]' to 'bool ** '        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 

There are three ways to skin the cat of dynamic 2d arrays. One way requires that you know one of the dimensions at compile time(which it doesn't appear you do). Another way requires doing the math yourself:
        bool *allocate (int width, int height){  return new bool[width * height];}bool &get_element (bool *array, int height, int x, int y){  return array[y * height + x];}  

This is the way I prefer to do it because there's less memory overhead than the next way. BTW, this begs to be put in a class, and in doing so you would be able to achieve "normal" 2d array semantics with a little thought.

The final way gives you natural way to access the array as two dimensional, but requires memory overhead:
  bool **allocate (int width, int height){  bool **array = new bool *[width];  for (int i=0; i < width; ++i)    array[i] = new bool[height];  return array;}  

This causes an extra array of pointers the size of your width to be used, but it gives you array[3][5] semantics automatically. Just be sure you clean up everything you new.

Edited by - Stoffel on August 28, 2001 1:02:29 PM
You know what... I''ve only given you the code that had to do with the errors, but here''s exactly what I have so far:

  typedef unsigned short int USHORT;class Block{public:	Block( USHORT getleftsrc , USHORT gettopsrc , USHORT width , USHORT height);	USHORT makeTetramino();private:	USHORT left;	USHORT top;	USHORT width;	USHORT height;	bool grid;};/*** CLASS METHODS ***/Block::Block( USHORT getleft , USHORT gettop , USHORT getwidth , USHORT getheight ){	left = getleft;	top = gettop;	width = getwidth;	height = getheight;	grid = new bool [ width ][ height ];}USHORT Block::makeTetramino(){	// I''m not sure what I''ll put in this function yet...	return 0;}/*** CLASS DEFINITIONS ***/Block Lblock( 256 , 0 , 2 , 3 );Block Tblock( 256 , 32 , 3 , 2 );Block Sqblock( 288 , 0 , 2 , 2 );Block Lnblock( 288 , 32 , 4 , 1 );Block Bwlblock( 320 , 0 , 2 , 3 );// I think you can guess what this program does....  


Now, when I put grid = new bool [ width ][ height ]; into the constructor, I get error C2540: non-constant expression as array bound, but when I make it one dimensional, like so: grid = new bool [ width ][ height ];, it compiles fine! What''s up with this?

I''ve tried everything suggested here.
"These books suck."-- Zidane
quote:
Original post by Gwargh
Now, when I put grid = new bool [ width ][ height ]; into the constructor, I get error C2540: non-constant expression as array bound , but when I make it one dimensional, like so: grid = new bool [ width ][ height ]; , it compiles fine! What''s up with this?

I''ve tried everything suggested here.

Er, no you haven''t, cos the previous poster already pretty much pointed out that you can''t use 2-dimensional stuff with ''new'' directly. Use [ width*height ] instead.

its so easy

you have to have

bool* grid; 


in the class definition, and have

grid = new bool[width*height]; 


in the constructor. thats it. do that, exactly.
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)

This topic is closed to new replies.

Advertisement