模版声明与实现分开//如何报无法解析

模版声明与实现分开//怎么报无法解析?
error LNK2019: 无法解析的外部符号 "public: __thiscall CTest<int>::~CTest<int>(void)" (??1?$CTest@H@@QAE@XZ),该符号在函数 _wmain 中被引用

error LNK2019: 无法解析的外部符号 "public: __thiscall CTest<int>::CTest<int>(void)" (??0?$CTest@H@@QAE@XZ),该符号在函数 _wmain 中被引用
1>c:\users\mirro187\documents\visual studio 2010\Projects\temp_nov13\Debug\temp_nov13.exe : fatal error LNK1120: 2 个无法解析的外部命令
1>


.h
template<typename T>
class CTest
{
public:
CTest(void);
~CTest(void);
void fun();
};



.cpp
#include "StdAfx.h"
#include "Test.h"

template <typename T>
CTest<T>::CTest(void)
{

}

template <typename T>
CTest<T>::~CTest(void)
{
}

template <typename T>
void CTest<T>::fun()
{

}



#include "stdafx.h"
#include "Test.h"

int _tmain(int argc, _TCHAR* argv[])
{
CTest<int> test;
return 0;
}

------解决方案--------------------
假如你定义了一个模板类 template<T> foo, 头文件在 foo.hpp中,源文件在foo.cpp中
这时你在bar.cpp中使用了foo<int>, 那么foo的代码会在 bar.cpp中展开, 因为你只include了foo.hpp,所以展开后的foo<int>类只有声明,没有实现。 于是编译器假定foo<int>的实现在其它的编译单元中,链接器会解决这个问题。

但问题是,foo<int>的实现不存于任何编译单元中,foo.cpp编译基本不生成任何符号,因为没有特化的模板代码只是模板,生不成可执行代码(连类型都不确定,没法生成),最终到链接器那里,链接器自然要发火了。

如果你在 foo.cpp 中对foo<int>进行特化(比如声明一个foo<int>的变量 foo<int> f;),那么链接应该是可以正确进行的,因为 foo.cpp 文件中对 foo<int> 进行展开时会连实现一起展开。