Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

RizMan

expected constant expression error

This topic is 5741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can''t figure something out, i.e. don''t know why I get this error. I will explain what I''m doing: I have a function LoadTextures(char *textureFiles[],int numTextures) This function I use to initialize the textures i.e. generate them with glGenTextures. So what I need is an array of the form GLUint textures[numTextures]; LoadTextures is called from the main Initialization function which is found in another file. So VC++ gives me following error: D:\Programming\NeHeGL\init.cpp(33) : error C2057: expected constant expression D:\Programming\NeHeGL\init.cpp(33) : error C2466: cannot allocate an array of constant size 0 That is I can''t use the "int numTextures" for the length of the "textures" array. In fact, when I put a constant in it everything works fine. However, as I do not know the number of textures that will be loaded, I certainly can''t hard Code it. So does anyone know why it does that. For testing purposes I did the exact same thing with a very simple function and it worked
  
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

void test(int i){           
    int j = 0;
    int test[i];        
    for (j = 0; j<i;j++){
        test[j] = j;
        printf("%d\n", test[j]);
    }
}

int main(int argc, char *argv[])
{
  int i = 10;
  test(i);
  system("PAUSE");	
  return 0;
}
  
However, it doesn''t seem to work with my more complex code

Share this post


Link to post
Share on other sites
Advertisement
Some compilers aren't very ANSI-C/C++ compatible (MSVC V6 especially) and won't like the:

int test [ i ];

construct, although, I believe, gcc copes OK with that one. You'll need to use new and create a dynamic array, thus:

int *a_test = new int [ i ];

which would be more compatible and won't cause you to run out of heap space. Don't forget to make sure you delete it:

delete [] a_test;

[Personal habit: I prefix dynamically allocated array names with 'a_' so that determining the correct delete to use is simple.]

Skizz


[edited by - Skizz on January 31, 2003 12:50:22 PM]

[edited by - Skizz on January 31, 2003 12:50:48 PM]

Share this post


Link to post
Share on other sites
Thank you for the fast reply. It works that way.

You''re right about gcc. The code from the test example has been compiled using gcc on my other Box. Whereas the original error came from VC++ 6.0.

Share this post


Link to post
Share on other sites
quote:
Original post by Skizz
Some compilers aren''t very ANSI-C/C++ compatible (MSVC V6 especially) and won''t like the:

int test [ i ];

That''s not ANSI/ISO C++: you can not define an array of non-constant length; any compiler that allows you to is providing compiler-specific extensions.

Otherwise, of course, you''re absolutely right

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!