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获取模板类中的实际类型”,但是小弟不是很理解。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值