我写了一个二叉树的类,如下:
在文件BinaryTree.h中:
template<typename elemname>
class BinaryTree{
public:
BinaryTree();
BinaryTree(const BinaryTree &);
~BinaryTree();
//--以下省略
构造函数和析构函数都在BinaryTree.cpp中实现:
#include"BinaryTree.h" template<typename elemname>//defualt constructor
BinaryTree<elemname>::BinaryTree():_root(0){} template<typename elemname>
BinaryTree<elemname>::BinaryTree(const BinaryTree<elemname> &rhs)
{copy(_root,rhs._root);} template<typename elemname>
BinaryTree<elemname>::~BinaryTree(){}
然后在主程序main.cpp中:
#include"BinaryTree.h"
void main(){
BinaryTree<int> bt;
}
结果编译器显示
main.obj : error LNK2001: unresolved external symbol "public: __thiscall BinaryTree<int>::~BinaryTree<int>(void)" (??1?$BinaryTree@H@@QAE@XZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall BinaryTree<int>::BinaryTree<int>(void)" (??0?$BinaryTree@H@@QAE@XZ)
但是把实现方法放入到BinaryTree.h中就可以 各位高手告诉一下究竟哪里错了,如何改正
在文件BinaryTree.h中:
template<typename elemname>
class BinaryTree{
public:
BinaryTree();
BinaryTree(const BinaryTree &);
~BinaryTree();
//--以下省略
构造函数和析构函数都在BinaryTree.cpp中实现:
#include"BinaryTree.h" template<typename elemname>//defualt constructor
BinaryTree<elemname>::BinaryTree():_root(0){} template<typename elemname>
BinaryTree<elemname>::BinaryTree(const BinaryTree<elemname> &rhs)
{copy(_root,rhs._root);} template<typename elemname>
BinaryTree<elemname>::~BinaryTree(){}
然后在主程序main.cpp中:
#include"BinaryTree.h"
void main(){
BinaryTree<int> bt;
}
结果编译器显示
main.obj : error LNK2001: unresolved external symbol "public: __thiscall BinaryTree<int>::~BinaryTree<int>(void)" (??1?$BinaryTree@H@@QAE@XZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall BinaryTree<int>::BinaryTree<int>(void)" (??0?$BinaryTree@H@@QAE@XZ)
但是把实现方法放入到BinaryTree.h中就可以 各位高手告诉一下究竟哪里错了,如何改正
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货