最近看算法书,想写一些通用的函数实现那些基本的算法,结果第一个就出问题了,如题目说的,C++中把模版函数的声明和定义分别写在了.h和.cpp文件中,导致编译出错。具体是这样的:
BasicAlgorithms.h文件:
#ifndef BASIC_ALGORITHMS_H
#define BASIC_ALGORITHMS_H
#include "stdafx.h"
#include <vector>
using namespace std;
class BasicAlgorithms
{
public:
template<typename Type> void InsertionSort(vector<Type> &);
};
#endif
然后在BasicAlgorithms.cpp文件中实现了InsertionSort()函数:
#include "stdafx.h"
#include "BasicAlgorithms.h"
template <typename Type>
void BasicAlgorithms::InsertionSort(vector<Type> &vec_to_sort)
{
vector<Type>::size_type i=0;
for(vector<Type::size_type j=2;j<vec_to_sort.size();j++