Static member function as wrapper -- Undefined symbols

Discussion in 'C++' started by Markus S, Jul 13, 2009.

  1. Markus S

    Markus S Guest

    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
     
    Markus S, Jul 13, 2009
    #1
    1. Advertising

  2. Markus S

    mzdude Guest

    On Jul 13, 10:19 am, Markus S <> wrote:
    > 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.
     
    mzdude, Jul 13, 2009
    #2
    1. Advertising

  3. Markus S

    Ron Guest

    On Jul 13, 10:19 am, Markus S <> wrote:

    > 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.
     
    Ron, Jul 13, 2009
    #3
  4. Markus S

    Markus S Guest

    On 2009-07-13 18:59:02 +0200, Ron <> said:

    > On Jul 13, 10:19 am, Markus S <> wrote:
    >
    >> 2) That DoIts function is defined as follows:
    >>      void DoIts(Testclass* pt2Objects, void (*pt2Function)(Testclas

    > s*
    >> 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.

    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
     
    Markus S, Jul 22, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    0
    Views:
    1,366
    Victor Bazarov
    Jun 23, 2003
  2. 0to60
    Replies:
    4
    Views:
    518
    jeffc
    Nov 21, 2003
  3. aling
    Replies:
    6
    Views:
    484
    Xiaobin.Huang
    Oct 30, 2005
  4. dolphin
    Replies:
    3
    Views:
    1,349
    Pete Becker
    Dec 5, 2007
  5. paul
    Replies:
    8
    Views:
    715
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page