Thread: I would like help with my basic callback function pointer library

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Casa Grande, AZ
    Posts
    4

    I would like help with my basic callback function pointer library

    Hi all,
    I am attempting to wrap my brain around function pointers. I created two files, exeCallback.h and exeCallback.cpp. I am receiving the error: "expression must have (pointer-to-) function type". The error resides at:
    Code:
    foo(2);
    After reading that error, I edited the line that was giving me the error to
    Code:
    exeCallback->foo(2);
    .
    Code:
    exeCallback->foo(2);
    throws two new errors that are, expected an identifier and expected a type specifier. What am I not doing to get my basic callback library to work successfully?

    exeCallback.h code:
    Code:
    /*
    File: exeCallback.h
    
    Header file for exeCommand Library.
    */
    
    #ifndef EXECALLBACK_H
    #define EXECALLBACK_H
    
    #include "mbed.h"
    
    #include <functional>
    
    #include <map>
    
    class exeCallback
    {
    public:
        exeCallback();
        
        void my_int_func(int x);
        
        void (exeCallback::*foo)(int);
    
    private:
    };
    
    #endif
    exeCallback.cpp file code:
    Code:
    /*
    File: exeCallback.cpp
    
    Execute functions in other Sensor libraries/classes
    
    Constructor
    */
    
    #include "mbed.h"
    #include "ConfigFile.h"
    #include "msExtensions.h"
    #include "cfExtensions.h"
    #include "exeCallback.h"
    
    exeCallback::exeCallback()
    {
        
        foo = &exeCallback::my_int_func;
    
        foo(2);
    
    }
    
    void exeCallback::my_int_func(int x)
    {
        printf( "%d\n", x );
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're not playing with pointers to functions. You're playing with pointers to members (it just so happens the member you're playing with is a member function). They are different things.

    To use a pointer to member it is necessary to explicitly tell the compiler what object the member is for. So ... foo(2) needs to be
    Code:
            (this->*foo)(2);
    or
    Code:
           (*this.*foo)(2);
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Casa Grande, AZ
    Posts
    4
    Quote Originally Posted by grumpy View Post
    You're not playing with pointers to functions. You're playing with pointers to members (it just so happens the member you're playing with is a member function). They are different things.

    To use a pointer to member it is necessary to explicitly tell the compiler what object the member is for. So ... foo(2) needs to be
    Code:
            (this->*foo)(2);
    or
    Code:
           (*this.*foo)(2);
    Thank you. Your explaination helped.
    Code:
    (this->*foo)(2);
    worked successfully.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic pointer in function question
    By Origin in forum C Programming
    Replies: 15
    Last Post: 04-01-2010, 10:14 AM
  2. Basic C question. What is a callback?
    By Subsonics in forum C Programming
    Replies: 8
    Last Post: 01-27-2009, 09:11 PM
  3. Replies: 3
    Last Post: 07-19-2008, 03:12 PM
  4. Basic program design, passing pointer to a function
    By heras in forum C Programming
    Replies: 14
    Last Post: 04-02-2008, 03:21 AM
  5. Pointer to member function as callback function
    By ninebit in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2002, 05:52 AM

Tags for this Thread