在VC6.0中是否无法编译通过普通类中的模板成员函数,详见内:

bigqin bigqin 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码:


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)



...全文
评论 6
收藏 已收藏
分享
返回顶部
bigqin
VC/MFC 16,550 成员 421,605 内容