模板类问题
日前练习STL时,写了一个模板Tree类:
//Head:
//没有slist.h
#include<list>
#include<algorithm>
using namespace std;
namespace MyAlgorithm
{
//template<class T> typedef void (const T&) DealFun;
template<class T> class Tree
{
public:
T Data;
list<Tree *> SunNodeList;
Tree():SunNode(slist<Tree *>(2)){};
Tree(const T& Value,int iMaxSunNodeCount = 2): Data(Value),SunNodeList(list<Tree *>(2)){};
Tree& AddSonWithValue(const T& Data)const;
Tree& AddSonWithNode(Tree* const& SonNode) const;
bool RemoveSonByIndex(const int& Index) const;
bool RemoveSonByValue(const T& Data) const;
void PreScan(void DealFun(T&)) const;
void PostScan(void DealFun(T&)) const;
};
}
//Source:
#include "Tree.h"
#include<list>
#include<algorithm>
using namespace std;
using namespace MyAlgorithm;
template<class T> Tree<T>& Tree<T>::AddSonWithValue(const T& Data) const
{
SonNodeList.push_back(new Tree<T>(Data));
return **(SonNodeList.back());
};
template<class T> Tree<T>& Tree<T>::AddSonWithNode(Tree<T> * const &SonNode) const
{
SonNodeList.push_back(SonNode);
return **(SonNodeList.back());
};
template<class T> bool Tree<T>::RemoveSonByValue(const T& Data) const
{
//The Way 1
for(list::iterator Iterator = SonNodeList.begin(); Iterator != SonNodeList.end(); ++Iterator)
{
if(Data == (*Iterator)->Data) list.earse(Iterator);
}
};
template<class T> bool Tree<T>::RemoveSonByIndex(const int& Index) const
{
list::iterator Iterator = SonNodeList.begin();
for(int i = 0; i < Index - 1; ++i) ++Iterator;
if(SonNodeList.end() != Iterator) SonNodeList.earse(Iterator);
};
template<class T> void Tree<T>::PreScan(void DealFun(T&)) const
{
DealFun(Data);
for(list::iterator Iterator = SonNodeList.begin(); Iterator != SonNodeList.end(); ++Iterator) (*Iterator)->PreScan(DealFun);
};
template<class T> void Tree<T>::PostScan(void DealFun(T&)) const
{
for(list::iterator Iterator = SonNodeList.begin(); Iterator != SonNodeList.end(); ++Iterator) (*Iterator)->PreScan(DealFun);
DealFun(Data);
};
//Test Code:
#include "Tree.h" // ..\Algorithm\
#include <iostream>
using namespace std;
using namespace MyAlgorithm;
void Fun4(int& i)
{
cout<<i<<'\t';
};
void main()
{
Tree<int> t(0);
t.AddSonWithValue(2);
t.AddSonWithValue(3);
t.AddSonWithValue(8);
t.PreScan(Fun4);
(**(t.SunNodeList.begin())).AddSonWithValue(6);
t.PostScan(Fun4);
};
对此程序,有以下问题,烦请大虾赐教:
1. 以上所有代码编译均通过,但无法链接,错误如下:
Linking...
Tree测试.obj : error LNK2001: unresolved external symbol "public: void __thiscall MyAlgorithm::Tree<int>::PostScan(void (__cdecl*)(int &))const " (?PostScan@?$Tree@H@MyAlgorithm@@QBEXP6AXAAH@Z@Z)
Tree测试.obj : error LNK2001: unresolved external symbol "public: void __thiscall MyAlgorithm::Tree<int>::PreScan(void (__cdecl*)(int &))const " (?PreScan@?$Tree@H@MyAlgorithm@@QBEXP6AXAAH@Z@Z)
Tree测试.obj : error LNK2001: unresolved external symbol "public: class MyAlgorithm::Tree<int> & __thiscall MyAlgorithm::Tree<int>::AddSonWithValue(int const &)const " (?AddSonWithValue@?$Tree@H@MyAlgorithm@@QBEAAV12@ABH@Z)
Debug/Test.exe : fatal error LNK1120: 3 unresolved externals
执行 link.exe 时出错.
请教如何处理此错误?是否与VC6对模板的实现有关?
2. 如何使用typedef定义模板类型?
若STL使用有不当之处,也请一并指正。谢谢!