Function Pointer to private member

I'm working on an assignment to access a private member function, but there is something wrong with my syntax. Here is what I got

The typedef and declaration in .h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class  Dictionary
{
  public  :

    typedef void  (*WordDefFunc)(const string  &word, const string  &definition);


    void  forEach( WordDefFunc  func, bool  forward );

  private  :

  struct words_def{
  string word;
  string define;
  };

  words_def *word_def_array;
  
  void show_array();


Calling the function Main

dict.forEach( dict.show_array , true );

Private member function definition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Dictionary::forEach( WordDefFunc  func, bool  forward )
{
	if (forward == true){
		show_array();
	}
}

void Dictionary::show_array()
{
for (int inc = 0 ; inc < wordNum ; ++ inc)
  {
     cout << word_def_array[inc].word   << endl;
     cout << word_def_array[inc].define << endl;
  }
}


This is the error VS 2012 gives me

error C3867: 'Dictionary::show_array': function call missing argument list; use '&Dictionary::show_array' to create a pointer to member

I've tried using the '&' in the function call but errors still occur.

Last edited on
typedef void (*WordDefFunc)(const string &word, const string &definition)
A function pointer to a free-standing function which takes two arguments of type const reference to std::string.

void Dictionary::show_array();
A member function that takes no arguments other than the hidden this pointer.

See any disparity between types there at all?
Yes, I see what you're saying. The assignment doesn't allow me to alter the public portion of the class. So my Words and Definitions are in the private section. For this function I need to simply display all the words and definitions using the function pointer and only having what's in the private section to work with.

Even if I changed the arguments in the show_array function, I'd still need arguments to send from Main, so I'm not sure what I'm suppose to be doing.
It seems to me that what you are doing is implementing the visitor pattern for your dictionary, and while show_array could use forEach in its implementation, I don't see why forEach would use show_array.

I would expect forEach's definition to look something like:

1
2
3
4
5
6
7
8
9
10
void Dictionary::forEach( WordDefFunc  func, bool  forward )
{
    if (forward == true)
    {
        for ( unsigned i=0; i<number_of_words; ++i )
            func(word_def_array[i].word, word_def_array[i].define) ;
    }
    else 
        // do it in reverse.
}


You could then use a free standing function:

1
2
3
4
 void print_it( const std::string& word, const std::string & def)
{
    std::cout << word << ":\n\t" << def << '\n' ;
}


like so:
1
2
3
4
5
int main()
{
    Dictionary d ;
    d.forEach(print_it, true) ;
}

Thank you, that was very helpful. I've been searching all over for an explanation on how this would work in my situation.
I implemented everything you suggested, and everything seems to work, but I'm still getting that same error. I'm calling for each from main like so

dict.forEach(dict.print_it, true) ;

I have to add the dict. to print_it otherwise I get a different compiler error.

C3867: 'Dictionary::print_it': function call missing argument list; use '&Dictionary::print_it' to create a pointer to member
When I said print_it was a free standing function, I meant that it was not a member of a class. The idea behind the visitor pattern is to allow code that isn't part of the class to manipulate or use data belonging to the class, within the confines of (in this case) the forEach method.
Last edited on
If you want to provide a method name ("member function" in C/C++) you'll need one of the pointer-to-member operators .* or ->*. See http://www.cplusplus.com/doc/tutorial/operators/. You may also need an object which may accept the method.
Topic archived. No new replies allowed.