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

bigqin 2004-04-07 10:19:35
代码:


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)



...全文
35 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ymbymb 2004-04-08
  • 打赏
  • 举报
回复
不是bug ,是VC6不支持模板嵌套
runner001_cn 2004-04-08
  • 打赏
  • 举报
回复
晕,我觉得是vc的bug,我也弄了好几天,都没有搞明白为什么,没有模板类就好了
Salmon2004 2004-04-07
  • 打赏
  • 举报
回复
对,应该按楼上的这样设计类。

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

template<class T>
class C
{
public:
C(){};
~C(){};
void test(int num);
};

#endif

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

template<class T>
void C<T>::test(int num)
{
T t;
t.print(num);
}

void main()
{
C<A> p1;
p1.test(2);

C<B> p2;
p2.test(2);
}
goodname 2004-04-07
  • 打赏
  • 举报
回复
#include <stdio.h>
class A
{
public:
void print(int i){printf("A::i = %d\n", i);}
};
class B
{
public:
void print(int i){printf("B::i = %d\n", i);}
};

template<class T>
class C
{
public:
void test(int num){T t;t.print(num);}
};
void main(void)
{
C<A> p;
p.test(2);
}

goodname 2004-04-07
  • 打赏
  • 举报
回复
#include <stdio.h>
class A
{
public:
static void print(int i){printf("A::i = %d\n", i);}
};
class B
{
public:
static void print(int i){printf("B::i = %d\n", i);}
};

template<class T>
class C
{
public:
void test(int num){T::print(num);}
};
void main(void)
{
C<A> p;
p.test(2);
}
Salmon2004 2004-04-07
  • 打赏
  • 举报
回复
gz

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧