在VC6.0中是否无法编译通过普通类中的模板成员函数,详见内:
代码:
1、test.h
#ifndef _TEST_H_
#define _TEST_H_
class A
{
public:
A(){};
~A(){};
static void print(int i);
};
class B
{
public:
B(){};
~B(){};
static void print(int i);
};
class C
{
public:
C(){};
~C(){};
template< class T >
void test(int num)
{
T::print(num);
};
};
template< class T >
void test(int num)
{
T::print(num);
};
#endif
2、test.cpp
#include <stdio.h>
#include "test.h"
void A::print(int i)
{
printf("A::i = %d\n", i);
}
void B::print(int i)
{
printf("B::i = %d\n", i);
}
void main()
{
C p;
p.test<A>(2); //在VC下有编译错误,用g++再linux下可以正常编译
p.test<B>(2); //在VC下有编译错误,用g++再linux下可以正常编译
}
在VC6.0下编译错误:
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
E:\tmp\test.cpp(19) : error C2275: 'A' : illegal use of this type as an expression
e:\tmp\test.h(5) : see declaration of 'A'
E:\tmp\test.cpp(20) : error C2275: 'B' : illegal use of this type as an expression
e:\tmp\test.h(15) : see declaration of 'B'
Error executing cl.exe.
test.exe - 2 error(s), 0 warning(s)