
Alain Leblanc wrote:
Hi,
I wrote some code where I want to iterate on the result of a regex matching and call a member function for each match. It goes roughly like this:
void XX::addDelayedVariables(string expr) { funcPtr = &XX::regex_callback; regex re("([[:alpha::]][[:alnum]]+)(\[[^\\]]+])"); sregex_iterator m1(expr.begin(), expr.end(), re); sregex_iterator m2; match_resultsstring::const_iterator results; (*) bool a = (this->*funcPtr)(results); (*) for_each(m1, m2, (this->*funcPtr)); (#) } ===========================================================================
When compiling (gcc 4.1 on FC 6) I get an error message at (#) that says: invalid use of non-static member function
I added the two lines with a (*) as a sanity check to see that I declared the pointer-to-member-function correctly, and I have no problem compiling those.
So my question is: is this a problem with me not understanding how to handle pointer-to-member-functions in C++, or is there something with the boost libraries that I'm missing or is preventing me from performing this operation? I would appreciate any help or opinion.
The problem is that (this->*funcPtr) is an unmentionable and unusable type in C++. Yes, you can use it at the site of a function call, but not as a functor. I'm sure Boost.Bind or Boost.Lambda have solutions, or you can use: std::bind1st(std::mem_fun(&XX::regex_callback), this) to create a valid functor to pass to for_each. HTH, John.