Question

I am getting compilation error in below code.

class A
{
public:
    A()
    {
    }
    ~A()
    {
    }
 void func()
 {
     cout <<"Ha ha ha \n";
 }

};

class C
{
public:
    C()
    {
    }
    ~C()
    {
    }
template<typename type> func()
{
    type t;
    t.func();
}
void callme()
{
    func<A>();
}
};
Was it helpful?

Solution

VC6 doesn't support member function templates. You actually have several solutions:

Move func() out of the class definition

template<typename type> void func()
{
    type t;
    t.func();
}

or rewrite callme()

void callme()
{
   A a;
   a.func();
}

or use class template

class C
{
public:
   template<class T> struct func
   {
      void operator()()
      {
         T t;
         t.func();
      }
   };

   void callme()
   {
      func<A>()();
   }
};

OTHER TIPS

The definition of func<T>() doesn't specify its return type, which is invalid in C++.

It should be:

template<typename type> void func()
{
    type t;
    t.func();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top