-
Forced to use constants
Hi,
I'm having a problem with being forced to use constants in my program.
At one moment, I try to do
AUX_RGBImageRec *TextureImage[nb_textures];
and my compiler complains that it was "expecting a constant expression". The problem is that I cannot define a constant with the right value because I don't know what that value is before runtime.
nb_textures is a member variable of a class and is set when the class is instantiated. I tried stuff like defining a constant in my class and initializing it to that same value but the compiler isn't dumb enough to be satisfied with that. I guess it follows the chain and sees that ultimately nb_textures is not a constant.
So I'm asking you guys how I can trick the compiler into thinking that it's given a constant. It's probably something really simple but I'm not really experienced and I am slowly learning the stuff.
Thanks in advance
-
When you create arrays statically, then you need constants. You can dynamically allocate your arrays:
Code:
AUX_RGBImageRec* TextureImage = new AUX_RGBImageRec[nb_textures];
and then deallocate when you are done
Code:
delete[] TextureImage;
A nicer, safer option, however, would be to use std::vector.
Code:
#include <vector>
... etc ...
std::vector<AUX_RGBImageRec> vec;
vec.push_back( ... an AUX_RGBImageRec here ... );
Then, you do not need to worry about deallocation. Also, you can force the vector to set aside enough space by calling the reserve(int) member function, taking the desired size as a parameter.
-
Thanks for the prompt reply. That made sense so I tried it but came up with a new error:
Code:
binary '=' : no operator found which takes a right-hand operand of type 'AUX_RGBImageRec *' (or there is no acceptable conversion)
This happens when I do
Code:
AUX_RGBImageRec* TextureImage = new AUX_RGBImageRec[nb_textures];
memset(TextureImage, 0, sizeof(void *) * nb_textures);
int nb_true_status = 0;
for( int i = 0; i < nb_textures; i++ )
{
if(TextureImage[i] = LoadBMP(TEXTURE_NB(i)))
{
nb_true_status++;
}
}
The error happens on the line that has the if statement. My guess is that there's a problem with the type but I've been working on this for so many hours that I can't think clearly anymore. The original
Code:
AUX_RGBImageRec *TextureImage[nb_textures];
seems to me like it defines an array of pointers to AUX_RGBImageRec objects if I'm not mistaken. The code that was suggested on the second post
Code:
AUX_RGBImageRec* TextureImage = new AUX_RGBImageRec[nb_textures];
doesn't seem to create the same in my understanding. I can't really follow what it's supposed to create. As soon as I got this fixed, I'm stopping for today ;)
*EDIT*
I tried using
Code:
AUX_RGBImageRec * TextureImage = (AUX_RGBImageRec*) malloc (sizeof(AUX_RGBImageRec *)*nb_textures);
but it gives the same mistake. Is it possible then that the error lies elsewhere?
Thanks
-
Is the first line of you code ("AUX_RGBImageRec* TextureImage = new AUX_RGBImageRec[nb_textures];") the line the error is complaining about? How is AUX_RGBImageRec defined? What does LoadBMP() return?
-
Oh I just reread your last post and see what you're saying. AUX_RGBImageRec is defined as a pointer and you are attempting to create an array of those pointers (a 2D array).
For a 2D array your syntax should look something like this:
YourPtrType *array = new YourPtrType[count];
where YourPtrType could be defined as something like:
typedef YourType* YourPtrType;
-
*EDIT* Oops looks like I started writing right before you answered. Well your solution fixes my problem. It makes a lot of sense.
Thanks a lot
-
Okay. You need to use a two-dimensional array. Each element of your array must be a pointer.
Code:
AUX_RGBImageRec **TextureImage = new AUX_RGBImageRec*[nb_textures];