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();
}
精彩评论