473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C function pointer within a class method


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

Jul 18 '07 #1
2 3994
hs******@gmail.com wrote:
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
So it should, a class member isn't a C linkage function, it has a hidden
this parameter and the wrong linkage type.

To pass a pointer to a function to a C library, you have to use a
vanilla function with extern "C" linkage

--
Ian Collins.
Jul 18 '07 #2
Ian Collins wrote:
hs******@gmail.com wrote:
>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
So it should, a class member isn't a C linkage function, it has a hidden
this parameter and the wrong linkage type.

To pass a pointer to a function to a C library, you have to use a
vanilla function with extern "C" linkage
Okay. I just did that. The only problem was that the function whose
pointer I am passing needed to use some member variables of the class.
That is why I had made that function as a member function as well.

Making that function (whose pointer I need) as vanilla function in C, I
got around the problem of using some member variables by saving them in
a structure and passing its pointer to the library function I am using.

It compiled ... the next stage is debugging.

Thanks for the help and explanation.
regards,
->HS

Jul 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
10
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
10
by: gmtonyhoyt | last post by:
It's been mentioned to me that, in standard c, that you can have structures behave very much like classes in relation to something to do with function calls within a structure. Now, I didn't get...
26
by: johkar | last post by:
I need to cancel the link and execute a function onclick of all the links within the span tag which has a class of "container" assigned. There will be only one span tag with this class applied. ...
20
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got...
8
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
43
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions...
5
by: pauldepstein | last post by:
Hi all, I saw some code like this: unsigned short SomeFunc(unsigned short SomeNum, bool SomeBool, const SomeClass& SomeMem, bool(SomeClass::*AmemberFunctionOfSomeClass)(const unsigned...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.