C function pointer within a class method

Discussion in 'C++' started by hs.samix@gmail.com, Jul 18, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    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.
    Ian Collins, Jul 18, 2007
    #2
    1. Advertising

  3. H.S. Guest

    Ian Collins wrote:
    > 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
    H.S., Jul 18, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian
    Replies:
    2
    Views:
    330
    Ulrich Petri
    Sep 6, 2003
  2. Mitko Haralanov
    Replies:
    17
    Views:
    420
    Ben Finney
    Oct 20, 2006
  3. Pawel_Iks
    Replies:
    3
    Views:
    522
    terminator
    Jul 31, 2007
  4. aidy
    Replies:
    5
    Views:
    221
    Marcin Mielżyński
    Jun 4, 2006
  5. Greg Hauptmann
    Replies:
    9
    Views:
    233
    Loren Segal
    Jun 16, 2008
Loading...

Share This Page