开发者

Error with member function template

开发者 https://www.devze.com 2023-01-29 09:19 出处:网络
I am getting compilation error in below code. class A { public: A() { } 开发者_开发知识库 ~A() { } void func()

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>();
}
};


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>()();
   }
};


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消