Having trouble trying to get the address of a member function

R

Ramon F Herrera

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;
}
 
R

Ramon F Herrera

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
 
R

Ramon F Herrera

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."
 
T

Thomas J. Gritzan

Sam said:
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.
 
T

Thomas J. Gritzan

Ramon said:
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) );
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top