C++ 函数模板创建

本文只是用于记录自己在学习中的错误和弱点。

习题16.52 题目要求使用函数模板count计算vector中某些值出现的次数。

1.类的声明

#pragma once
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

class MidSearch
{
public:
	MidSearch(void);	
	~MidSearch(void);
	template <typename T> int countT(vector<T>::iterator itBeg, vector<T>::iterator itEnd, T &t);
	
};

 2.模板函数的定义

template <typename T> 
int MidSearch::countT(vector<T>::iterator itBeg, vector<T>::iterator itEnd, T &t) 
//error C2998: "int countT"不能是模板定义
{...}

 修改:

template <typename T, typename Tr> int countT(T itBeg, T itEnd, Tr &t);

 酱紫的话就要声明两个模板实参了,如何只用一个呢?求高人解答。。悲催的书上貌似没有。。

 

参考链接:

http://topic.csdn.net/u/20101019/14/1d57996e-19a9-4654-b3cc-936a0a1a35f5.html

 

里面有说道“用T::SUB_TYPE_T获取模板类中的实际类型”,但是小弟不是很理解。。

你可能感兴趣的:(C++,函数模板)