Sign in to follow this  
evolutional

[Solved] GCC Templated Functor problems

Recommended Posts

I'm trying to compile my Manta-X project under GCC 3.3.4 and have run into some problems with the messaging system that my Entities are relying upon. The project was coded mainly in VC++7 and I received no problems at all, so I'm wondering if I'm not quite adhering to some standard or something. So far I've had a few errors and wranings thrown back at me, but they're mainly to do with reinforcing const references - this one is alluding me. The message functor template class is defined as:
class IEntityMessageFunctor : public IMMO
{
public:
  virtual int operator()(sEntityMessageInfo *msg) = 0;  // call using operator
  virtual int Call(sEntityMessageInfo *msg) = 0;        // call using function
};

template <class TClass> class TEntityMessageFunctor : public IEntityMessageFunctor
{
private:
  typedef int (TClass::*FUNCTOR)(sEntityMessageInfo *msg);   // pointer to member function
  TClass* pt2Object;                  // pointer to object
  FUNCTOR fpt;
public:
  TEntityMessageFunctor ( TClass* _pt2Object, FUNCTOR _fpt )
  { 
   pt2Object = _pt2Object;
   fpt=_fpt; 
  }
   // SNIP
};


In my Entity class I have a member function int Entity::OnUpdate( sEntityMessageInfo *msg). The problem occurs when I register the message with my handler, or more specifically when I'm creating a new instance of the templated functor eg: IEntityMessageFunctor *test = new TEntityMessageFunctor<Entity>(this, OnUpdate); The errors I'm getting from GCC follow:
Entity.cpp: error: no matching function call to
 MantaX::TEntityMessageFunctor<MantaX::Entity>::TEntityMessageFunctor( MantaX::Entity* const, <unknown type>)

Candidates are:
 MantaX::TEntityMessageFunctor<MantaX::Entity>::TEntityMessageFunctor(const MantaX::TEntityMessageFunctor<MantaX::Entity>& )



The thing is, I'm stumped. I've played around with it for a while and just can't figure it out - google's not helping much either so I thought I'd throw it out here. Does anyone know what I'm doing wrong here? Thanks -Oli [Edited by - evolutional on December 6, 2004 4:56:16 AM]

Share this post


Link to post
Share on other sites
Did you try changing:

IEntityMessageFunctor *test = new TEntityMessageFunctor<Entity>(this, OnUpdate);

to:

IEntityMessageFunctor *test = new TEntityMessageFunctor<Entity>(this, &Entity::OnUpdate);

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Did you try changing:

IEntityMessageFunctor *test = new TEntityMessageFunctor<Entity>(this, &Entity::OnUpdate);


SiCrane, you just made my Christmas giftlist. I think I tried everything but that, Thank you :)

Is there any reason why GCC will choke on that but VC++ doesn't? Is it just down to standards compliance?

Share this post


Link to post
Share on other sites
Basically, without doing the &Entity:: part, OnUpdate by itself is of type member function. A member function type can decay to either a reference to a member function or a pointer to a member function, but the pointer to a member function decay is only performed if there is an unambiguous use of the object as a pointer to member function type. However, in the case of template function instantiation, the unambiguous use isn't there, so gcc spazzes. By putting the &Entity:: part in, you make the OnUpdate explicitly of type pointer to member function and everything clicks for the compiler.

But, yeah, it's a real pain in the rear to figure out what's going on the first time it happens to you.

edit: spelling

[Edited by - SiCrane on December 6, 2004 5:01:38 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this