Jump to content
  • Advertisement
Sign in to follow this  
Jarwulf

passing arrays of references

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

Alright I think I screwed up somewhere in this process. I'm trying to find a way to pass ObjectArray. But I keep getting errors such as left should be class union struct, cannot instantiate object, and arrays of refs are illegal depending on what I put.
int main()
{
bot Basicbot;
hero firsthero;
collisionmachine collisionmachineinst;

object *ObjectArray[2];

ObjectArray[0]=&firsthero;//both inherit off object
ObjectArray[1]=&Basicbot;



collisionmachineinst.Collisionf(ObjectArray[2]);//not sure if this is right
}





class collisionmachine
{

void Collisionf(object *ObjectArray[2])//not sure about this either
{
 if(Collision::BoundingBoxTest(*ObjectArray[x].Mass,*ObjectArray[y].Mass)==1)//err
}
};

Share this post


Link to post
Share on other sites
Advertisement
. binds tighter than *, so you need to write

(*ObjectArray[x]).Mass

or

ObjectArray[x]->Mass

Oh, and in the line where you write "//not sure if this is right", you need to get rid of the [2].

Share this post


Link to post
Share on other sites
if you want to send the array

collisionmachineinst.Collisionf(ObjectArray);

should work if it does not try changing your Collisionf function to


void Collisionf(object **ObjectArray){
}


also *pointer.x will probably give an error. try (*pointer).x or better pointer->x

Share this post


Link to post
Share on other sites
void Collisionf(object **ObjectArray) and
void Collisionf(object *ObjectArray[2]) are completely equivalent.

Share this post


Link to post
Share on other sites
1) That isn't an array of references; it's an array of pointers. You can't make an array of references.

2) The [2] is not part of the array's name; it is part of the array's type. Therefore, omit it when using the array (e.g. passing it to the function). After all, when you write 'ObjectArray[x]' in the function's implementation, it means 'the xth element of ObjectArray'... so when you write ObjectArray[2] in main, it's interpreted in the same way. And there is no element [2] of a 2-element array (only elements 0 and 1).

3) 'object *ObjectArray[2]' is fine for specifying the parameter when you define the function. So is 'object** ObjectArray'. They will actually translate into the same thing. The array size from the first version is ignored.

4) As noted, use ObjectArray[x]->Mass.

Share this post


Link to post
Share on other sites
As an interesting side note: If you had(could have) an array of references in a game with just a tad more than two objects, you would run out of memory faster than you can say 'segfault'.

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!