天天看點

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擷取模闆類中的實際類型”,但是小弟不是很了解。。

繼續閱讀