读书人

关于类模板的解决办法

发布时间: 2012-05-20 16:03:12 作者: rapoo

关于类模板的
我在 megersort.h 文件里定义了

template <class ElementType>
class MegerSort
{
public:
void Sort(ElementType element[],int n);
private:
void Msort(ElementType element[],ElementType tmp[],int left,int right);
void Meger(ElementType element[],ElementType tmp[],int lpos,int rpos,int rightend);
};

我在main.cpp 里面包含了megersort.h文件


#include "MegerSort.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
MegerSort<int> M;
int tmp[10];
int i;
for(i=0;i!=10;++i)
cin>>tmp[i];
M.Sort(tmp,10);
for(i=0;i!=10;++i)
cout<<tmp[i]<<'\ ';
system("pause");
}

但是编译失败

main.obj : error LNK2019: unresolved external symbol "public: void __thiscall MegerSort<int>::Sort(int * const,int)" (?Sort@?$MegerSort@H@@QAEXQAHH@Z) referenced in function _main
1>F:\数据结构\分治法\Debug\分治法.exe : fatal error LNK1120: 1 unresolved externals
1>

这个实在是搞不懂。

[解决办法]
你函数的实现呢?就申明了下没有实现搞什么?还有就是模板需要定义和实现放在一个文件里。

读书人网 >C++

热点推荐