c++-faq-lite - Question about pointer-to-member function example

M

Markus S

Hi,

in this FAQ
(http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.7)
an example is given of how to 'use' a pointer to a member function. I
have trouble even implementing a subset of it:

--------- Fredmain.cpp ------------
#include "Fred.h"
typedef int (Fred::*FredMemFn)(char x, float y);

int main()
{
FredMemFn p = &Fred::f;
}
--------------------------------------------

---------------- Fred.h ------------------
class Fred {
public:
Fred();
~Fred();

int f(char x, float y);
};
--------------------------------------------

---------------- Fred.cc -----------------
#include "Fred.h"

Fred::Fred() {}
Fred::~Fred() {}

int f(char x, float y)
{
return 5;
}
--------------------------------------------

Compiling this I get:

Undefined symbols:
"Fred::f(char, float)", referenced from:
__ZN4Fred1fEcf$non_lazy_ptr in Fredmain.o
ld: symbol(s) not found

which the line 'FredMemFn p = &Fred::f;' triggers. I admit I do not
really understand the syntax of his typedef, which is why I probably
mess things up.
Thanks for any clarification.

Markus
 
R

Ron

#include "Fred.h"

Fred::Fred() {}
Fred::~Fred() {}

   int f(char x, float y)
   {
   return 5;
   }
--------------------------------------------
\

This doesn't declare Fred::f, but a non-member function
at namespace scope called f.

You want

int Fred::f(char x, float y) { ...
 
M

Markus S

\

This doesn't declare Fred::f, but a non-member function
at namespace scope called f.

You want

int Fred::f(char x, float y) { ...
Thanks, in my quest to make the declaration in the .h and .cc file the
same, I used copy and paste too much and forgot to add Fred:: after the
copying (or rather used another file as a template where I had commited
this error).
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top