C++ 函数指针类型与 Linux 和 VMS 上的候选不兼容

标签 c++ linux function-pointers vms

这个问题与我的另一个问题有关- boost::bind return a function object which is the argument for a function that requires pointer

除了

的界面
bridge_set_pound_var_func

不允许更改。

此外,boost::functionboost::bind 不适用于大型项目。

我的新代码如下:

#include <iostream>

class myA
{
 public:

 int bridge_set_pound_var_func(int (*fp)(const char *, char *, void *), void *arg)
 {
     void * b = NULL;
     int a = fp("this is poundVar", "ths is t1", b) ;
     std::cout << "bridge_set_pound_var_func is called "<< " , a is " << a << std::endl ;
     return 0;

 }

};

class myC
{
public:
myA *myOA;
int func(const char * poundVar , char * t1, void * t2);

int myCCall()
{
    myA myAO;
    myOA = &myAO;
    std::cout << "myCCall is called " << std::endl;

    myOA->bridge_set_pound_var_func( &myC::func, (void *)this );

    return 0;

 }

};

int myC::func(const char * poundVar , char * t1, void * t2)
{
 std::cout << "myC::func is called " << std::endl;
 return 1;

}

int main()
{
  myC myCO ;
  myC *m1p = &myCO ;
  m1p->myCCall() ;

  return 0 ;
}

// EOF

Linux 上的错误:

  In member function 'int myC::myCCall()':

  error: no matching function for call to 'myA::bridge_set_pound_var_func(int (myC::*)(const char*, char*, void*), void*)'

  candidates are: int myA::bridge_set_pound_var_func(int (*)(const char*, char*, void*), void*)

VMS 上的错误:

 In member function 'int myC::myCCall()':

 error: no matching function for call to 'myA::bridge_set_pound_var_func(int (myC::*)(const char*, char*, void*), void*)'

 candidates are: int myA::bridge_set_pound_var_func(int (*)(const char*, char*, void*), void*)

最佳答案

简短的回答是:指向成员函数的指针不是指向函数的指针。前者需要了解调用它们的对象,而后者则不需要。使用的典型方法是使用通常存在的“用户数据”void* 指向合适的基类,转换指针并调用相应的虚函数。从那里您可以轻松地恢复必要的对象上下文。

关于C++ 函数指针类型与 Linux 和 VMS 上的候选不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12441596/

相关文章:

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - 如何开始 bho(浏览器帮助对象)编程

linux - 内核中关于工作队列的一些标志

linux - insmod错误: inserting './hello.ko' : -1 Invalid module format"

c++ - 未签名的 WINAPI 函数的正确函数指针是什么?

c++ - 一个跟踪插入顺序的 std::map ?

c++ - 提取模板类中的类型以用于其他模板

linux - 需要捕获 Linux 上触发的命令

c++ - 等价于 C++ 中的 C NULL 函数指针?

C++ 和成员函数指针