Having trouble trying to get the address of a member function

Discussion in 'C++' started by Ramon F Herrera, Sep 18, 2009.

  1. Hello all,

    This is my first non-trivial, multi-file C++ program with classes and
    I bumped into a problem. See example from the Boost.Regex library
    below.

    The problem occurred after I converted the simple code below to a
    class (called "Pairs"). The troublesome statement is this one, where
    we attempt to take the address of a function.

    std::for_each(m1, m2, &regex_callback);

    My first compilation attempt produces an error message from the
    compiler, which is nice enough to offer a solution:

    pairs.cpp:247: error: ISO C++ forbids taking the address of an
    unqualified or parenthesized non-static member func tion to form a
    pointer to member function. Say `&Pairs::assignment_callback'

    So, following the compiler's suggestion, I changed the line above to
    this:

    std::for_each(m1, m2, &Pairs::assignment_callback);

    Now, I am getting an inscrutable (for me, anyway) error message:

    stl_algo.h:158: error: must use .* or ->* to call pointer-to-member
    function in `__f (...)'

    Thanks for your kind assistance,

    -Ramon


    ===================================================

    int main(int argc, const char** argv)
    {
    std::string text;
    for(int i = 1; i < argc; ++i)
    {
    cout << "Processing file " << argv << endl;
    std::ifstream fs(argv);
    load_file(text, fs);
    // construct our iterators:
    boost::sregex_iterator m1(text.begin(), text.end(), expression);
    boost::sregex_iterator m2;
    std::for_each(m1, m2, &regex_callback); <== This code does not
    work inside a Class.
    // copy results:
    cout << class_index.size() << " matches found" << endl;
    map_type::iterator c, d;
    c = class_index.begin();
    d = class_index.end();
    while(c != d)
    {
    cout << "class \"" << (*c).first << "\" found at index: " <<
    (*c).second << endl;
    ++c;
    }
    class_index.erase(class_index.begin(), class_index.end());
    }
    return 0;
    }
    Ramon F Herrera, Sep 18, 2009
    #1
    1. Advertising

  2. On Sep 17, 10:47 pm, Sam <> wrote:
    > Ramon F Herrera writes:
    > > std::for_each(m1, m2, &Pairs::assignment_callback);

    >
    > > Now, I am getting an inscrutable (for me, anyway) error message:

    >
    > > stl_algo.h:158: error: must use .* or ->* to call pointer-to-member
    > > function in `__f (...)'

    >
    > The third parameter is a function object, not a function pointer.
    >
    > Specify "std::ptr_fun(&Pairs::assigment_callback)".
    >
    >  application_pgp-signature_part
    > < 1KViewDownload



    Thanks, Sam!

    After doing some reading, it looks like I did not provide enough
    information. This is the signature of the callback function:

    bool Pairs::assignment_callback(const
    boost::match_results<std::string::const_iterator>& what)

    And therefore, several examples I have found have lead me to believe
    that a typedef like this will be useful, if not required:

    typedef bool (Pairs::*CallbackFunction) (const
    boost::match_results<std::string::const_iterator>&);

    Tried your suggestion, but it looks like the types of arguments have
    to be added somehow.

    Thanks!

    -Ramon
    Ramon F Herrera, Sep 18, 2009
    #2
    1. Advertising

  3. On Sep 17, 9:51 pm, Ramon F Herrera <> wrote:
    > Hello all,
    >
    > This is my first non-trivial, multi-file C++ program with classes and
    > I bumped into a problem. See example from the Boost.Regex library
    > below.
    >
    > The problem occurred after I converted the simple code below to a
    > class (called "Pairs"). The troublesome statement is this one, where
    > we attempt to take the address of a function.
    >
    > std::for_each(m1, m2, &regex_callback);
    >
    > My first compilation attempt produces an error message from the
    > compiler, which is nice enough to offer a solution:
    >
    > pairs.cpp:247: error: ISO C++ forbids taking the address of an
    > unqualified or parenthesized non-static member func tion to form a
    > pointer to member function.  Say `&Pairs::assignment_callback'
    >
    > So, following the compiler's suggestion, I changed the line above to
    > this:
    >
    > std::for_each(m1, m2, &Pairs::assignment_callback);
    >
    > Now, I am getting an inscrutable (for me, anyway) error message:
    >
    > stl_algo.h:158: error: must use .* or ->* to call pointer-to-member
    > function in `__f (...)'
    >
    > Thanks for your kind assistance,
    >
    > -Ramon
    >
    > ===================================================
    >
    > int main(int argc, const char** argv)
    > {
    >   std::string text;
    >   for(int i = 1; i < argc; ++i)
    >   {
    >   cout << "Processing file " << argv << endl;
    >   std::ifstream fs(argv);
    >   load_file(text, fs);
    >   // construct our iterators:
    >   boost::sregex_iterator m1(text.begin(), text.end(), expression);
    >   boost::sregex_iterator m2;
    >   std::for_each(m1, m2, &regex_callback);   <== This code does not
    > work inside a Class.
    >   // copy results:
    >   cout << class_index.size() << " matches found" << endl;
    >   map_type::iterator c, d;
    >   c = class_index.begin();
    >   d = class_index.end();
    >   while(c != d)
    >   {
    >   cout << "class \"" << (*c).first << "\" found at index: " <<
    > (*c).second << endl;
    >  ++c;
    >  }
    >   class_index.erase(class_index.begin(), class_index.end());
    >   }
    > return 0;
    >
    > }
    >
    >


    My problem is described here:

    http://www.parashift.com/c -faq-lite/pointers-to-members.html

    Still, I not too sure about the syntax required for compilation to
    succeed.

    -Ramon

    "I strongly recommend these features. In the real world, member
    function invocations are a lot more complex than the simple example
    just given, and the difference in readability and writability is
    significant. comp.lang.c++ has had to endure hundreds and hundreds of
    postings from confused programmers who couldn't quite get the syntax
    right. Almost all these errors would have vanished had they used these
    features."
    Ramon F Herrera, Sep 18, 2009
    #3
  4. Sam schrieb:
    > Ramon F Herrera writes:
    >
    >> On Sep 17, 10:47 pm, Sam <> wrote:
    >>> Ramon F Herrera writes:
    >>> > std::for_each(m1, m2, &Pairs::assignment_callback);
    >>>
    >>> > Now, I am getting an inscrutable (for me, anyway) error message:
    >>>
    >>> > stl_algo.h:158: error: must use .* or ->* to call pointer-to-member
    >>> > function in `__f (...)'
    >>>
    >>> The third parameter is a function object, not a function pointer.
    >>>
    >>> Specify "std::ptr_fun(&Pairs::assigment_callback)".
    >>>
    >>> application_pgp-signature_part
    >>> < 1KViewDownload

    >>
    >>
    >> Thanks, Sam!
    >>
    >> After doing some reading, it looks like I did not provide enough
    >> information. This is the signature of the callback function:
    >>
    >> bool Pairs::assignment_callback(const
    >> boost::match_results<std::string::const_iterator>& what)
    >>
    >> And therefore, several examples I have found have lead me to believe
    >> that a typedef like this will be useful, if not required:
    >>
    >> typedef bool (Pairs::*CallbackFunction) (const
    >> boost::match_results<std::string::const_iteratorw>&);
    >>
    >> Tried your suggestion, but it looks like the types of arguments have
    >> to be added somehow.

    >
    > On the contrary. The function receives the element in the container that
    > std::for_each is iterating over.
    >
    > Somehow, I doubt that your container contains
    > boost::match_results<std::string::const_iterator> objects.


    No. The problem is that a member function pointer cannot be called with
    normal function call syntax, so for_each doesn't work with member
    function pointers directly.
    You'd have to use a function object wrapper instead.

    --
    Thomas
    Thomas J. Gritzan, Sep 18, 2009
    #4
  5. Ramon F Herrera schrieb:
    > Hello all,
    >
    > This is my first non-trivial, multi-file C++ program with classes and
    > I bumped into a problem. See example from the Boost.Regex library
    > below.
    >
    > The problem occurred after I converted the simple code below to a
    > class (called "Pairs"). The troublesome statement is this one, where
    > we attempt to take the address of a function.
    >
    > std::for_each(m1, m2, &regex_callback);
    >
    > My first compilation attempt produces an error message from the
    > compiler, which is nice enough to offer a solution:
    >
    > pairs.cpp:247: error: ISO C++ forbids taking the address of an
    > unqualified or parenthesized non-static member func tion to form a
    > pointer to member function. Say `&Pairs::assignment_callback'
    >
    > So, following the compiler's suggestion, I changed the line above to
    > this:
    >
    > std::for_each(m1, m2, &Pairs::assignment_callback);
    >
    > Now, I am getting an inscrutable (for me, anyway) error message:
    >
    > stl_algo.h:158: error: must use .* or ->* to call pointer-to-member
    > function in `__f (...)'


    Pairs::assignment_callback is a member function of a class. When you
    call this function, you have to provide an instance of the class. The
    std::for_each function doesn't know this and tries to call every
    function or functor you pass as third parameter with normal function
    call syntax. The solution is to pass a wrapper that is callable like a
    normal function but actually calls a member function.

    The easiest way to do this ist boost::bind:

    std::for_each(m1, m2, boost::bind(&Pairs::assignment_callback, this, _1) );

    --
    Thomas
    Thomas J. Gritzan, Sep 18, 2009
    #5
    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. Roy Yao
    Replies:
    3
    Views:
    491
    Shane Beasley
    Sep 28, 2003
  2. Roy Yao
    Replies:
    0
    Views:
    349
    Roy Yao
    Sep 28, 2003
  3. Replies:
    3
    Views:
    543
    Greg Comeau
    Nov 17, 2005
  4. Mike
    Replies:
    9
    Views:
    360
  5. blackbiscuit
    Replies:
    12
    Views:
    1,332
    blackbiscuit
    May 13, 2009
Loading...

Share This Page