Cannot convert parameter problem

  • Thread starter Thread starter Drew
  • Start date Start date
D

Drew

I have the following code in a header file (pseudocode):

template <class T>
class Pt3 : public Vec3<T>
{
public:
// Constructors
Pt3() {}

void setVertFcn( void (*vf)(T x, T y, T z)) { vertfcn = vf; }
}

and in another header:

class SplineCurve
{
public:
CurveD() { Pt3<double> pt; pt.setVertFcn( glVertex3d); }
}

Compiler complains:

error C2664: 'Pt3<T>::setVertFcn' : cannot convert parameter 1 from 'void
(GLdouble,GLdouble,GLdouble)' to 'void (__cdecl *)(T,T,T)'
with
[
T=double
]
and
[
T=double
]
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast

So following the compilers suggestion:

SplineCurveD() { Pt3<double> pt; pt.setVertFcn( (void (__cdecl
*)(T,T,T))&glVertex3d); }

Error becomes:

error C2664: 'Pt3<T>::setVertFcn' : cannot convert parameter 1 from 'void
(__cdecl *)(T,T,T)' to 'void (__cdecl *)(T,T,T)'
with
[
T=double
]
and
[
T=double
]
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast

What's the right way to do this cast?

Thanks,
Drew
 
I still receive a similar error:

CurveD() { Pt3<GLfloat> pt; pt.setVertFcn( &glVertex3d); }

error C2664: 'Pt3<T>::setVertFcn' : cannot convert parameter 1 from 'void
(__stdcall *)(GLdouble,GLdouble,GLdouble)' to 'void (__cdecl *)(T,T,T)'

It seems a calling convention problem. Can that even be cast or do I need
to change
my declaration? (void setVertFcn( void (*vf)(T x, T y, T z)) { vertfcn =
vf; })

Thanks,
Drew
 
__cdecl is the default calling convention, while __stdcall is used to call Win32 API functions. If you right click the project, select properties, select Advanced under the C++ node you can change it. I haven't tried, but by the look of it, functions that use it need a function prototype

You might be able to declare the function pointer as __stdcal

int (__stdcall *ptr)(int a, …)

But like I said, I haven't tried it

I've seen COM using it

class A : public ComThin

virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv
â€


But I haven't tried that either
 
yes, you should change
void setVertFcn( void (*vf)(T x, T y, T z) ) { vertfcn = vff;
to
typedef void (*VF)(T x, T y, T z)
VF vertfcn
void setVertFcn( VF vff) { vertfcn = vff;

maybe the problem was that vertfcn was not properly declared in the example of your first post
 
Thanks! Using your suggestion plus casting the input arg in the
call fixed my problem.

Thanks again,
Drew
 
Back
Top