简体   繁体   中英

Pointer passing to function header

I have another problem regarding pointers.

I have a function with the following header:

 addActor (NxuPhysicsCollection &c, NxActor &a, const char *userProperties=0, const char *actorId=0)

And I am trying to use it like this:

 NXU::NxuPhysicsCollection* collection = new NXU::NxuPhysicsCollection();
 NxActor* actor = *actors++;
 NXU::addActor(collection, actor, NULL, NULL);

But I get the following error:

A reference of type "NXU::NxuPhysicsCollection&" (not const-qualified) cannot be initialised with a value of type "NXU::NxuPhysicsCollection*" (this is for the collection parameter, same error appears for actor as well)

How am I supposed to pass the collection and actor parameters to the function in order to work properly?

I tried this:

 NXU::addActor(&collection, &actor, NULL, NULL);

But that doesn''t work either, it gives me the error:

 "Initial value of reference to non-const must be a lvalue."

Any help is greatly appreciated.

Edit: If I use it like this:

NXU::addActor((NXU::NxuPhysicsCollection&)collection, (NxActor&)actor, NULL, NULL);

It does not give me errors anymore. Is this correct?

I must mention that NXU and NX namespaces are closed source and I cannot modify the way they are implemented

Both the c and a parameters are references .

Pass them as:

NXU::addActor(*collection, *actor, NULL, NULL);

This act is called pointer-dereference.

Since the formal arguments of your member function for c and a are references , while collection and actor are pointers , you need to dereference pointers to make them compatible with references:

// No need to pass NULLs for the defaulted parameters, so
// the trailing NULL, NULL are removed.
NXU::addActor(*collection, *actor);

Alternatively, you can change the signature of the constructor to accept pointers instead of references:

addActor (NxuPhysicsCollection *c, NxActor *a, const char *userProperties=0, const char *actorId=0)
//                             ^           ^
//                             |           |
//                             Here and Here
NXU::addActor(*collection, *actor, NULL, NULL);

because

 addActor (NxuPhysicsCollection &c, NxActor &a, const char *userProperties=0, const char *actorId=0)

receives collection and actor by reference , not by pointer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM