Trying to declare an array in a class constructor
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
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
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
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
August 28, 2001 02:39 AM
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.
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
Wow, I want whatever compiler you guys are using. You can't pass two non-constants directly into a dynamic memory allocation:
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:
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:
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
|
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:
|
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:
|
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:
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.
|
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement