Forced to use constants

This is a discussion on Forced to use constants within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm having a problem with being forced to use constants in my program. At one moment, I try to ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    13

    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

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    13
    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
    Last edited by Night_Blade; 08-08-2005 at 05:13 PM.

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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?

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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;
    Last edited by LuckY; 08-08-2005 at 05:31 PM.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    13
    *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
    Last edited by Night_Blade; 08-08-2005 at 05:31 PM.

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-12-2008, 11:00 AM
  2. define constants
    By shuo in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 04:17 PM
  3. COnstants
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2007, 05:59 AM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  5. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21