首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

模板的有关问题,编译出错,请问

2012-02-09 
模板的问题,编译出错,请教这是我的类声明,放在bintree.h文件里templateclassTclassCBintree{public:CBin

模板的问题,编译出错,请教
这是我的类声明,放在bintree.h文件里
template   <class   T>
class   CBintree  
{
public:
CBintree();
~CBintree();
struct   TreeNode  
{
T   va;
TreeNode   *plchild;
TreeNode   *prchild;
};
//VOID   DepthFS();//深度遍历
//VOID   WidthFS();     //层次遍历
protected:
private:
TreeNode   *m_pBintree;
};

类函数,放在bingtree.cpp文件里
#include   <iostream.h>
#include   <stack>
using   namespace   std;

#include   "bintree.h "

template   <class   T>
CBintree <T> ::CBintree()
{
}

template   <class   T>
CBintree <T> ::~CBintree()   //先序释放二叉树
{
}

这是main函数放在main.cpp文件里
#include   <iostream.h>
#include   <stack>
using   namespace   std;

#include   "bintree.h "
int   main()
{
CBintree <int>   bt;
return   0;
}

build的时候报错如下
Linking...
main.obj   :   error   LNK2001:   unresolved   external   symbol   "public:   __thiscall   CBintree <int> ::~CBintree <int> (void) "   (??1?$CBintree@H@@QAE@XZ)

main.obj   :   error   LNK2001:   unresolved   external   symbol   "public:   __thiscall   CBintree <int> ::CBintree <int> (void) "   (??0?$CBintree@H@@QAE@XZ)
Debug/bintree.exe   :   fatal   error   LNK1120:   2   unresolved   externals
Error   executing   link.exe.

把bingtree.cpp的函数定义挪道main.cpp里没有报错,请问为什么?望赐教!!

[解决办法]
google “模板分离编译”
[解决办法]
就目前的情况来说,还是最好把模版的声明与实现都放在h文件里

热点排行