Re: member functions

New Message Reply Date view Thread view Subject view Author view

From: Alexandre Naaman (naaman++at++laplace.engr.sgi.com)
Date: 05/31/2000 02:35:10


On Tue, 30 May 2000, Ken Lindsay wrote:

Hi Ken,

>
> hi,
>
> I am trying to implement traverser callbacks in a c++ class. If I try to
> make the function a member function, I get this error:
>
> "NelAircraft.cpp", line 333: error(1515): a value of type
> "void (NelAircraft::*)(pfuTraverser *)" cannot be assigned to an
> entity of type "pfuTravFuncType"
> trav.preFunc = foo_preTravCB;
> ^
>
>
> if I prototype the functions separately:
>
> int foo_postTravCB(pfuTraverser *trav);
>
>
> then I compile with no problem. How am I supposed to write this to
> get the c++ compiler to be happy?

Your callback cannot be a non-static method of your class. An example
should make things clear:

class NelAircraft {
public:
  int cb0(pfuTraverser *trav);
  static int cb1(pfuTraverser *trav);

  ....
};

then doing:

        trav.preFunc = cb0;

will not work while:

        trav.preFunc = NelAircraft::cb1;

will. You may want to add a pointer to the this pointer as the data for
your pfuTraverser:

        trav.data = this; // or use ptr to your object of type
                          // 'NelAircraft' if not performing this in a
                          // method of NelAircraft.

and then in NelAircraft::cb1() do:

int NelAircraft::cb1(pfuTraverser *trav)
{
        NelAircraft *obj = (NelAircraft *)trav->data;
        
        // insert whatever you need to do in your traversal here

        return PTRAV_CONT; // or whatever...
}

et voila!

So, what this really comes down to is that the definition for a
pfuTraverser.preFunc != to what you were giving. In other words:

int (*)(pfuTraverser *) != void (NelAircraft::*)(pfuTraverser *)

(that's the one liner explanation for why you're getting the compiler
error :)

A+,

Alex.
   

--
Alexandre Naaman - naaman++at++sgi.com - La conformité est la mort de l'âme.


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Thu Jun 01 2000 - 12:11:52 PDT

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.