为什么不能编译,帮帮我!!!!
最近我在学习模板,做了一个测试程序,但是在连接出错。代码如下:
test2.h
template <class T>
class test2
{
public:
test2();
virtual ~test2();
};
test2.cpp
// test2.cpp: implementation of the test2 class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "test2.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
template <class T>
test2<T>::test2()
{
}
template <class T>
test2<T>::~test2()
{
}
testMain.cpp
#include "test2.h"
void main()
{
// TODO: Place code here.
test2<char> a;
return 0;
}
compile 通过,
连接时,显示如下错误:
error LNK2001: unresolved external symbol "public: virtual __thiscall test2<char>::~test2<char>(void)" (??1?$test2@D@@UAE@XZ)
我选的是VC++6。0中project生成向导中的“Win32 Console Application”
是不是编译器没设置好?
问题点数:100、回复次数:9Top
1 楼LookSail(老得牙都掉了还在学)回复于 2002-10-03 17:11:23 得分 20
你把这个模板类的头文件加进来再看一下Top
2 楼stonespace(stonespace)回复于 2002-10-03 17:12:50 得分 40
template的成员函数的实现,应当写在h文件中。
Top
3 楼afc(afc)回复于 2002-10-03 17:55:18 得分 0
virtual ~test2();去掉virtual试试
Top
4 楼caitou123(自向红尘取烦恼)回复于 2002-10-03 18:00:45 得分 20
楼上说的是。将.pp中的代码拷到.h放在一起,再删掉.pp文件Top
5 楼King_0119(智者无悔)回复于 2002-10-03 18:00:58 得分 0
大哥
模板能重载么!Top
6 楼ccaommao(草帽)回复于 2002-10-03 18:07:52 得分 0
gzTop
7 楼alpha_Ouyang(oyq)回复于 2002-10-03 21:43:30 得分 0
to:LookSail(ff)
照你说的做了,可以。但是为什么呢?能不能保留cpp呢,按理是应该有cpp的。Top
8 楼everandforever(Forever)回复于 2002-10-03 21:48:02 得分 20
你在CSDN上搜索“模板”,可以搜到一堆帖子,里面就有说明为什么一定要放在一起的。Top
9 楼alpha_Ouyang(oyq)回复于 2002-10-03 22:18:00 得分 0
谢谢各位,给分了Top




