Jump to content
  • Advertisement
Sign in to follow this  
CJWR

dumb question (arrays in c++)

This topic is 4836 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

what the heck am i doing wrong here?
int test[3];
test[0] = 1;
error messages: error C2466: cannot allocate an array of constant size 0 error C2501: 'test' : missing storage-class or type specifiers error C2086: 'test' : redefinition error C2440: 'initializing' : cannot convert from 'const int' to 'int []' i know it is these 2 lines of code, i just can't seem to figure out what i'm doing wrong with this... thank you for pointing out what i did wrong.

Share this post


Link to post
Share on other sites
Advertisement
It must be in another part of your program, because the following compiles fine for me:

int main() {
int test[3];
test[0] = 1;
}

Based on what you posted, I'd say you have another variable named test in that same function. In cases like this, it helps immensly to post all of the code.

Share this post


Link to post
Share on other sites
Just to make sure, what other lines are in that function? Those two lines by themselves should be legal...

Share this post


Link to post
Share on other sites
Its not those 2 lines of code, is somewhere before those, perhaps one containing "int test[0];"

Share this post


Link to post
Share on other sites
..
Judging from the error messages, I would say that those two lines are globally scoped. You cannot assign values in the global namespace. When the compiler sees "test[0] = 1" it thinks you're declaring a new array called "test" of unknown type, with a size of "0" and trying to assign the constant "1" to it. Hence the four error messages you're getting.

If you want to initialize your array with constant values, you could do something like:


int test[3] = {1,0,0};


Share this post


Link to post
Share on other sites
For the array of constant size zero you may be initializing an array on the stack somewhere that does not use a constant(either a const variable or just a numeric value) as the array size. It doesn't look like it's the code you've posted as others have said.
error #2 should be because there was a problem when making test, and error 3 should be because of the place you define test earlier in your code, probably where the non-const array size is given.
I'm not exactly sure about error 4 without more code I guess.

Like the poster above me said, error 4 is probably where you define the const int '1' to the array that it thinks you're initializing, but I'm not sure really.

Share this post


Link to post
Share on other sites
Quote:
Original post by pragma Fury
..
Judging from the error messages, I would say that those two lines are globally scoped. You cannot assign values in the global namespace. When the compiler sees "test[0] = 1" it thinks you're declaring a new array called "test" of unknown type, with a size of "0" and trying to assign the constant "1" to it. Hence the four error messages you're getting.

If you want to initialize your array with constant values, you could do something like:


int test[3] = {1,0,0};


ah! thats it! thank you. i always thought you could assign values in global. i guess you learn something new everyday. thanks all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!