Static member function as wrapper -- Undefined symbols

M

Markus S

Hi,

following the example described in
http://www.newty.de/fpt/callback.html#member, I set up a static member
function as a wrapper to hand over a function pointer to a member
function. This works fine as long as I call the wrapper function from
outside its class, when I call it from inside (after adjusting some
types), I get this error:
Undefined symbols:
"Testclass::DoIts(Testclass*, void (*)(Testclass*, double*))",
referenced from:
Testclass::CallTrickyF() in Testclass.o
ld: symbol(s) not found

The code that works is:

------------- Wrap.cpp ---------------
#include "Testclass.h"
#include <iostream>

void* pt2Object;
double x = 6.0;
double* y = &x;
void DoIt(void* pt2object, void (*pt2Function)(void* pt2Object, double* y))
{
pt2Function(pt2Object, y);
}
int main()
{
Testclass one;
DoIt((void*) &one, Testclass::Wrapper_TrickyF);
return 0;
}
--------------------------------------------

----------- testclass.h -----------------
using namespace std;
class Testclass
{
public:
Testclass();
~Testclass();
double TrickyF(double* x);
static void Wrapper_TrickyF(void* pt2Object, double* x);
};
-------------------------------------------

--------------- testclass.cc -----------
#include "Testclass.h"
#include <iostream>

Testclass::Testclass() {}
Testclass::~Testclass() {}

double Testclass::TrickyF(double* x)
{
return (*x-5.0);
}

void Testclass::Wrapper_TrickyF(void* pt2Object, double* x)
{
Testclass* mySelf = (Testclass*) pt2Object;
mySelf->TrickyF(x);
}
------------------------------------------

The problem starts when I want to move the DoIt function into my class.
1) I declare and define a function in my testclass.h and testclass.cc
files that calls a new DoIts function inside my Testclass class:
void CallTrickyF()
{
Testclass tempc = *this;
DoIts(&tempc, Testclass::Wrapper_TrickyFs);
}
2) That DoIts function is defined as follows:
void DoIts(Testclass* pt2Objects, void (*pt2Function)(Testclass*
pt2Objects, double* Y))
{
pt2Function(pt2Objects, Y);
}
3) I add the declaration of these three variables to my class
Testclass* pt2Objects;
double X = 7.0;
double* Y = &X;
4) I define a new wrapper which takes a pointer to an object of type
Testclass (instead of void);
void Testclass::Wrapper_TrickyFs(Testclass* pt2Objects, double* Y)
5) And I call 'CallTrickyF' from 'main':
one.CallTrickyF();

Everything is compiled with bjam, the Jamroot file is straightforward:
exe testwrap
: Wrap.cpp Testclass.cc
: <include>.
;

Any idea what is wrong with it or any examples of working code with
such wrapper functions?
Thanks.

Markus
 
M

mzdude

Hi,

following the example described inhttp://www.newty.de/fpt/callback.html#member, I set up a static member
function as a wrapper to hand over a function pointer to a member
function. This works fine as long as I call the wrapper function from
outside its class, when I call it from inside (after adjusting some
types), I get this error:
Undefined symbols:
  "Testclass::DoIts(Testclass*, void (*)(Testclass*, double*))",
referenced from:
      Testclass::CallTrickyF()      in Testclass.o
ld: symbol(s) not found

The code that works is:

Irrelevant. Show us the code that doesn't work. Also helpful would
be which compiler and version is having the problem.
 
R

Ron

2) That DoIts function is defined as follows:
     void DoIts(Testclass* pt2Objects, void (*pt2Function)(Testclass*
pt2Objects, double* Y))
     {
                pt2Function(pt2Objects, Y);
      } TrickyF();

Are you sure you declared this right. If this isn't inside the class
definition, DoIts needs to read Testclass::DoIts above.
 
M

Markus S

Are you sure you declared this right. If this isn't inside the class
definition, DoIts needs to read Testclass::DoIts above.
You are correct, I made the same mistake a several times (you had
pointed it out to me in reply to another post of mine).
Thanks,
Markus
 

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

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top