Problems with C and C++

A

Andreas Lassmann

Hi there,
can someone tell me how this code should look like that I can compile it?

dSpaceCollide(this->space, 0, &CPhysics::NearCallback); // The method I
like to use

void CPhysics::NearCallback(void *data, dGeomID o1, dGeomID o2) // The
method I've to call

ERROR: cannot convert `void (CPhysics::*)(void*, dxGeom*, dxGeom*)' to
`void (*)(void*, dxGeom*, dxGeom*)' for argument `3' to `void
dSpaceCollide(dxSpace*, void*, void (*)(void*, dxGeom*, dxGeom*))' // The
error I get.

NearCallback usually was a C function but I like to use it as a method. As
some background info: NearCallback is a function from Open-Dynamics-Engine
(www.ode.org). I like to use it in a game...

THANKS IN ADVANCE!
 
J

Janusz Szpilewski

Andreas Lassmann said:
Hi there,
can someone tell me how this code should look like that I can compile it?

dSpaceCollide(this->space, 0, &CPhysics::NearCallback); // The method I
like to use

void CPhysics::NearCallback(void *data, dGeomID o1, dGeomID o2) // The
method I've to call

ERROR: cannot convert `void (CPhysics::*)(void*, dxGeom*, dxGeom*)' to
`void (*)(void*, dxGeom*, dxGeom*)' for argument `3' to `void
dSpaceCollide(dxSpace*, void*, void (*)(void*, dxGeom*, dxGeom*))' // The
error I get.

You cannot do it. C++ compiler will not accept a member function where a C
function is expected. Eventually you might try Managed C++ but if you just
do not want to create standalone functions declare callback handlers as
static class members.

Regards,
Janusz
 
?

=?ISO-8859-15?Q?Ney_Andr=E9_de_Mello_Zunino?=

Andreas said:
void CPhysics::NearCallback(void *data, dGeomID o1, dGeomID o2) // The
method I've to call

ERROR: cannot convert `void (CPhysics::*)(void*, dxGeom*, dxGeom*)' to
`void (*)(void*, dxGeom*, dxGeom*)' for argument `3' to `void
dSpaceCollide(dxSpace*, void*, void (*)(void*, dxGeom*, dxGeom*))' // The
error I get.

NearCallback usually was a C function but I like to use it as a method. As
some background info: NearCallback is a function from Open-Dynamics-Engine
(www.ode.org). I like to use it in a game...

You are trying to pass a pointer to a non-static member function where a
pointer to a function is expected. That is not possible, as a non-static
member function requires an instance of the class on which to be
invoked. Only static member functions could be used in such a context.
Read more about it in the C++ FAQ [1] (check out the 3 first items).

[1] http://www.parashift.com/c++-faq-lite/pointers-to-members.html

Regards,
 
P

Peter Koch Larsen

Andreas Lassmann said:
Hi there,
can someone tell me how this code should look like that I can compile it?

dSpaceCollide(this->space, 0, &CPhysics::NearCallback); // The method I
like to use

void CPhysics::NearCallback(void *data, dGeomID o1, dGeomID o2) // The
method I've to call

ERROR: cannot convert `void (CPhysics::*)(void*, dxGeom*, dxGeom*)' to
`void (*)(void*, dxGeom*, dxGeom*)' for argument `3' to `void
dSpaceCollide(dxSpace*, void*, void (*)(void*, dxGeom*, dxGeom*))' // The
error I get.

NearCallback usually was a C function but I like to use it as a method. As
some background info: NearCallback is a function from Open-Dynamics-Engine
(www.ode.org). I like to use it in a game...

THANKS IN ADVANCE!

Just read the errormessage:
You pass an void (CPhysics::*)(void*, dxGeom*, dxGeom*) to a function that
expects a
void (*)(void*, dxGeom*, dxGeom*). The function you pass is a
memberfunction, but a freestanding function was expected. In essense the
function you pass expects four parameters (one loosely speaking being the
"this" pointer). You can solve it by using boost::bind.


/Peter
 
M

msalters

Peter Koch Larsen schreef:
Just read the errormessage:
You pass an void (CPhysics::*)(void*, dxGeom*, dxGeom*) to a function that
expects a
void (*)(void*, dxGeom*, dxGeom*). The function you pass is a
memberfunction, but a freestanding function was expected. In essense the
function you pass expects four parameters (one loosely speaking being the
"this" pointer). You can solve it by using boost::bind.

Only if you control dSpaceCollide(). However, if the void* in void
(*)(void*, dxGeom*, dxGeom*) is the 0 parameter from the dSpaceCollide
call, the
solution is simple. Pass (this) instead of 0, and provide a
static CPhysics::NearCallbackWrapper(void* _this, dxGeom*, dxGeom*)
which calls CPhysics::NearCallbackWrapper(dxGeom*, dxGeom*) on
(CPhysics*) _this.

HTH,
Michiel Salters
 
P

Peter Koch Larsen

msalters said:
Peter Koch Larsen schreef:

Only if you control dSpaceCollide(). However, if the void* in void
(*)(void*, dxGeom*, dxGeom*) is the 0 parameter from the dSpaceCollide
call, the
solution is simple. Pass (this) instead of 0, and provide a
static CPhysics::NearCallbackWrapper(void* _this, dxGeom*, dxGeom*)
which calls CPhysics::NearCallbackWrapper(dxGeom*, dxGeom*) on
(CPhysics*) _this.

HTH,
Michiel Salters
Right

I overlooked the fact that the function wanted a "plain old" function.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top