Passing function as argument

Discussion in 'C++' started by steven_orocos@vanderbeke.be, Nov 27, 2006.

  1. Guest

    Hi,

    I'm tryin to pass a funtion as an argument in another funtion.

    This code works
    ----------------------------------
    typedef void (*func)(int);

    void test1(int a){cout<<"1";}
    void test2(int a){cout<<"2";}
    void test3 (void (*fp) (int)) {
    (fp)(5);
    }
    void test() {
    func pf;
    pf = test1;
    test3(test1);
    }
    --------------------------------
    But when I put the cide in a class, it won't work anymore.
    -------------------------------
    typedef void (*func)(int);

    void MyClass::test1(int a){cout<<"1";}
    void MyClass::test2(int a){cout<<"2";}
    void MyClass::test3 (void (*fp) (int)) {
    (fp)(5);
    }
    void MyClass::test() {
    func pf;
    pf = test1;
    test3((func)test1);
    }
    -----------------------
    Gives me the error
    --------------
    error: argument of type 'void (MyClass::)(int)' does not match
    'void (*)(int)'
    error: invalid use of member (did you forget the '&' ?)
    --------------------

    I've tried several things, like
    test3(&test1);
    or
    typedef void (MyClass::*func)(int);


    But i can't find the solution.
    Also the net gives me several different solution, but none work..
     
    , Nov 27, 2006
    #1
    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. Reckoner
    Replies:
    11
    Views:
    704
    Steven D'Aprano
    Jan 19, 2009
  2. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
  3. Trans
    Replies:
    3
    Views:
    198
    Robert Klemme
    Jan 26, 2005
  4. ±è ÁØ¿µ
    Replies:
    7
    Views:
    254
    Einar Magnús Boson
    Nov 27, 2008
  5. User1014
    Replies:
    1
    Views:
    186
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page