Jump to content
  • Advertisement
Sign in to follow this  
chad_420

More Template woes

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this code(which compiles under gcc) here:
class Functor : public IMMObject
{
public:
   virtual void operator ()()=0;
};

template<class T>
class ObjFunctor : public Functor
{
   protected:
      T *obj;
      typedef void (T::*funcType)();
      funcType func;
   public:
      AUTO_SIZE;
   
      ObjFunctor(T *o, funcType f) 
      { obj=o; func=f; }
   
      void operator ()() 
      { (obj->*func)(); }
};

template<class T>
class MMObjFunctor : public Functor
{
protected:
   CMMPointer<T> obj;
   typedef int (T::*funcType)();
   funcType func;
public:
   AUTO_SIZE;

   MMObjFunctor(T *o, funcType f) 
   { obj=o; func=f; }

   int operator ()() 
   { return (obj->*func)(); }
};

But it produces errors in msvc6 And I dot really understand the error message can anyone give me some insight... I get this error: d:\src\msvceg\utils.h(46) : error C2555: 'MMObjFunctor<T>::()' : overriding virtual function differs from 'Functor::()' only by return type or calling convention d:\src\msvceg\utils.h(9) : see declaration of 'Functor' d:\src\msvceg\utils.h(47) : see reference to class template instantiation 'MMObjFunctor<T>' being compiled

Share this post


Link to post
Share on other sites
Advertisement
In C++, you can't overload functions by return type only. Consider the following:

int Func()
{
return 7;
}
float Func()
{
return 3.14f;
}

int main()
{
Func();
return 0;
}



Which function do I want to call? Overloading by return type only is ambiguous, and generally speaking, it won't let you do it. In MMObjFunctor, the operator() has a return type of int, while there is already an existing virtual function with a return type of void. You're trying to create a second function with the same name that differs only by return type, and the compiler isn't letting you.

Share this post


Link to post
Share on other sites
I see...


class Functor : public IMMObject
{
public:
virtual int operator ()()=0;
};

template<class T>
class ObjFunctor : public Functor
{
protected:
T *obj;
typedef int (T::*funcType)();
funcType func;
public:
AUTO_SIZE;

ObjFunctor(T *o, funcType f)
{ obj=o; func=f; }

int operator ()()
{ return (obj->*func)(); }
};

template<class T>
class MMObjFunctor : public Functor
{
protected:
CMMPointer<T> obj;
typedef int (T::*funcType)();
funcType func;
public:
AUTO_SIZE;

MMObjFunctor(T *o, funcType f)
{ obj=o; func=f; }

int operator ()()
{ return (obj->*func)(); }
};




is the correct way. I got this code from the Enginuity articles. Its very different in the articles than in the provided source. Thanks for your help though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!