以下是三个函数的申明
typedef sb4 (*OCICallbackInBind)(dvoid *ictxp, OCIBind *bindp, ub4 iter,
ub4 index, dvoid **bufpp, ub4 *alenp,
ub1 *piecep, dvoid **indp);
#define OCICallbackDefine ocidcfp
sword OCIBindDynamic ( OCIBind *bindp,
OCIError *errhp,
dvoid *ictxp,
OCICallbackInBind (icbfp)(/*_
dvoid *ictxp,
OCIBind *bindp,
ub4 iter,
ub4 index,
dvoid **bufpp,
ub4 *alenp,
ub1 *piecep,
dvoid **indpp */),
dvoid *octxp,
OCICallbackOutBind (ocbfp)(/*_
dvoid *octxp,
OCIBind *bindp,
ub4 iter,
ub4 index,
dvoid **bufpp,
ub4 **alenpp,
ub1 *piecep,
dvoid **indpp,
ub2 **rcodepp _*/) );
当我在C++中使用时,用到了
OCIBindDynamic(bndhp[14], errhp, (dvoid *) &pos[4], cbf_no_data,
(dvoid *) &pos[4], cbf_get_data);
其中cbf_no_data是参数中第四个变量,是一个函数
但是总是出现下面的问题
error C2664: OCIBindDynamic : cannot convert parameter 4 from int (void *,struct OCIBind *,unsigned int,unsigned int,void ** ,unsigned int *,unsigned char *,void ** ) to int (__cdecl *)(void *,str
uct OCIBind *,unsigned int,unsigned int,void ** ,unsigned int *,unsigned char *,void ** )
None of the functions with this name in scope match the target type
怎样解决?
因为类成员函数指针和非成员函数指针的类型是不相同的,请你在对C++有了一定的认识以后在试图将C代码向C++代码转移。
你的问题根本就不是C代码在C++下不能正常编译,二是你对C++的class的概念不清引起的。
如果你完全使用旧的C代码,显然他们是不可能使用类的成员函数作为第四个参数的,C++的编译器可以通过编译。
对于这两种类型的函数指针的区别,可以给你一点提示:
class MyClass
{
void foo();
}
void foo();
typedef void (* pfunc1)();
typedef void (MyClass::*pfunc2)();
pfunc1 func1=foo;
pfunc2 func2=MyClass::foo;
很显然,pfunc1和pfunc2是两个完全不同的类型,C++当然不能在这两者之间进行转型。
从编译器实现的角度来说,成员函数调用时需要有编译器将class的this指针传递给函数,这样实际的调用栈和你从源代码中的直观看到的不相同,而静态成员函数属于类而不是对象,所以不需要隐含的this指针,这样他的调用栈和费成员函数相同。
所以
class MyClass
{
static void foo2();
};
typedef void (* pfunc3)();
pfunc3 func3=MyClass::foo2;
这里,func3的类型就和func2完全一致,当然可以编译通过。
普通成员函数不能作为回调函数,只有静态的成员函数和全局函数才能做回调函数