Jump to content
  • Advertisement
Sign in to follow this  
bobsaget

Problem with an array

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

This is a really easy question and i am not thinking straight right now. I create an array bool[] blockhitarray; bool alive = true; and initialize all of its elements with for (int i = 0; i < 100; i++) { blockhitarray = alive; } I am getting an error telling me that i am not setting the array up properly. Object reference not set to an instance of an object. That is the error message. Not sure why it is not initializing. It is probably something so rudimentary and I am not thinking of it. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Assuming you're using C++, you are indeed declaring your array incorrectly.

The following should do:
bool blockHitArray[100];
for(int i = 0; i < 100; i++)
blockHitArray = true;


Don't you have a book or other reference that explains these basics?

Share this post


Link to post
Share on other sites


bool blockHitArray[100];



This is the right way to declare the array, but if you still want to use alive as the true value, what I suggest doing is using #define.

Which gives:


#define alive true//up near the top




bool blockHitArray[100];

for(int i = 0; i < 100; i++)

blockHitArray = alive;



Share this post


Link to post
Share on other sites
Quote:
Original post by MortusMaximus
This is the right way to declare the array, but if you still want to use alive as the true value, what I suggest doing is using #define.

It will still work as he has written it though, with a local variable. #defining alive will make it a global and might introduce hard to find errors since the preprocessor will just replace all instances of the string alive with true, which might not always be what he wants. If you need a global constant in C++ you should declare it as such:

const bool alive = true;

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!