模板类问题

yedaoq 2008-12-01 05:17:56
日前练习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使用有不当之处,也请一并指正。谢谢!

...全文
66 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
healer_kx 2008-12-02
  • 打赏
  • 举报
回复
?
healer_kx 2008-12-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yedaoq 的回复:]
谢LS二位,果然系此问题,看来VC6对的模板的支持....
[/Quote]
Sorry,不是VC6的问题,是所有的C++的编译器的问题,而且未来的决议是:不在把分离作为问题处理了。
就应该如此的。
yedaoq 2008-12-02
  • 打赏
  • 举报
回复
谢LS二位,果然系此问题,看来VC6对的模板的支持....
wzaen 2008-12-01
  • 打赏
  • 举报
回复
楼主,把模板的函数放到.h文件里面,试试。
healer_kx 2008-12-01
  • 打赏
  • 举报
回复
模板暂时不支持export关键字,也不支持声明和实现分离,你还是写在一个.h文件里面吧。

PS:以后也不可能实现。

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧