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:oIts(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
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:oIts(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