#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;
}
expected constant expression error
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
However, it doesn''t seem to work with my more complex code
Some compilers aren't very ANSI-C/C++ compatible (MSVC V6 especially) and won't like the:
int test ;<br><br>construct, although, I believe, gcc copes OK with that one. You'll need to use new and create a dynamic array, thus:<br><br>int *a_test = new int ;<br><br>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:<br><br>delete [] a_test;<br><br>[Personal habit: I prefix dynamically allocated array names with 'a_' so that determining the correct delete to use is simple.]<br><br>Skizz<br><br><br><SPAN CLASS=editedby>[edited by - Skizz on January 31, 2003 12:50:22 PM]</SPAN> <br><br><SPAN CLASS=editedby>[edited by - Skizz on January 31, 2003 12:50:48 PM]</SPAN>
int test ;<br><br>construct, although, I believe, gcc copes OK with that one. You'll need to use new and create a dynamic array, thus:<br><br>int *a_test = new int ;<br><br>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:<br><br>delete [] a_test;<br><br>[Personal habit: I prefix dynamically allocated array names with 'a_' so that determining the correct delete to use is simple.]<br><br>Skizz<br><br><br><SPAN CLASS=editedby>[edited by - Skizz on January 31, 2003 12:50:22 PM]</SPAN> <br><br><SPAN CLASS=editedby>[edited by - Skizz on January 31, 2003 12:50:48 PM]</SPAN>
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.
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.
quote:Original post by Skizz
Some compilers aren''t very ANSI-C/C++ compatible (MSVC V6 especially) and won''t like the:
int test ;</tt> <hr height=1 noshade></SPAN></BLOCKQUOTE> <br>That''s not ANSI/ISO C++: you <strong>can not</strong> define an array of non-constant length; any compiler that allows you to is providing compiler-specific extensions.<br><br>Otherwise, of course, you''re absolutely right <img src="smile.gif" width=15 height=15 align=middle>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement