passing arrays of references

Started by
4 comments, last by Rakshasa 14 years, 6 months ago
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
}
};

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].
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
taytay
void Collisionf(object **ObjectArray) and
void Collisionf(object *ObjectArray[2]) are completely equivalent.
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.
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'.
Doesn't matter how much you lose as long as you make sure the winner loses enough blood.

This topic is closed to new replies.

Advertisement