Hello,
I am trying to use a library which has a function, lets call it an
external function, which wants a function pointer as one of its
arguments.
If I use that library in a C program, all works well and I have no
problems. But I used the C program only to get started with the
library. The actual program I want to use that library with is in C++.
In my C++ program, I have a method function whose pointer I want to
use as the function pointer in the external function. However, I am
running in to problems. g++ gives this error (source code is given
below):
$g++ -ansi -pedantic -Wall -o classfoo classfoo.cc
classfoo.cc: In member function 'void ClassFoo::Method()':
classfoo.cc:30: error: cannot convert 'void (ClassFoo::*)(int*)' to
'void (*)(int*)' in assignment
Below is an example program which demonstrates the above problem. In
this, vFunc1 is the external function which I cannot change (for the
curious, I am trying to use this library:
http://www.ics.forth.gr/~lourakis/levmar/,
and the function I am trying to use is dlevmar_dif on that web page).
//------------------------ classfoo.cc
---------------------------------------
#include <iostream>
//a pre-defined function; not to be changed
void vFunc1(void (*func)(int *j), int i){
int m=0;//initialized to certain value
func(&m);
std::cout << m + i << std::endl;
}
//our example class
class ClassFoo{
public:
int q;
ClassFoo();
void Method();
void ModMethod(int *j);
};
ClassFoo::ClassFoo(){
q = 2;
}
void ClassFoo::Method(){
void (*FuncPtr)(int *) = NULL;
FuncPtr = &ClassFoo::ModMethod;
//vFunc1(FuncPtr,q);
}
void ClassFoo::ModMethod(int *j){
(*j) = (*j) + 2;//modify value
return;
}
int main(){
ClassFoo EgClass;
EgClass.Method();
return 0;
}
//---------------------------------------------------------------------------------
So, how do I get around this problem?
->HS