类模版和函数模版需要注意的

  1. // test.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <string>  
  7.   
  8. using namespace std;  
  9.   
  10. /* 
  11.  * 函数模版的格式为: template<class T> 函数名(函数参数); 
  12.  * 模版函数的实现要和声明放在一起,如果不放在一起,在实现的时候要重新声明template<class T>  
  13.  */  
  14. template<class T>  
  15. void add(T a, T b)  
  16. {  
  17.     T temp = 0;  
  18.     temp = a + b;  
  19.     cout << temp << endl;  
  20. }  
  21.   
  22. template<class T>  
  23. void sub(T a, T b)  
  24. {  
  25.     T temp = 0;  
  26.     temp = a-b;  
  27.     cout << temp << endl;  
  28. }  
  29.   
  30.   
  31. int _tmain(int argc, _TCHAR* argv[])  
  32. {  
  33.     int num1 = 1;  
  34.     int num2 = 2;  
  35.     double db1 = 22.33;  
  36.     double db2 = 33.55;  
  37.     add(num1, num2);  
  38.     add(db1, db2);  
  39.     sub(db1, db2);  
  40.     system("pause");  
  41.     return 0;  
  42. }  

 

[cpp] view plain copy
  1. // test.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <string>  
  7.   
  8. using namespace std;  
  9.   
  10. /* 
  11.  * 函数模版的格式为: template<class T> 函数名(函数参数); 
  12.  * 模版函数的实现要和声明放在一起 
  13.  * 如果不放在一起,在实现的时候要重新声明template<class T> 
  14.  * 之所以这样的原因是,编译的过程中编译器遇到template<class T> 
  15.  * 之后,才知道下边的类或者函数是一个特殊的函数模版或者类模版 
  16.  * 
  17.  */  
  18. template<class T>  
  19. void add(T a, T b);  
  20.   
  21. template<class T>  
  22. void sub(T a, T b);  
  23.   
  24. int _tmain(int argc, _TCHAR* argv[])  
  25. {  
  26.     int num1 = 1;  
  27.     int num2 = 2;  
  28.     double db1 = 22.33;  
  29.     double db2 = 33.55;  
  30.     add(num1, num2);  
  31.     add(db1, db2);  
  32.     sub(db1, db2);  
  33.     system("pause");  
  34.     return 0;  
  35. }  
  36. template<class T>  
  37. void sub(T a, T b)  
  38. {  
  39.     T temp = 0;  
  40.     temp = a-b;  
  41.     cout << temp << endl;  
  42. }  
  43.   
  44. template<class T>  
  45. void add(T a, T b)  
  46. {  
  47.     T temp = 0;  
  48.     temp = a + b;  
  49.     cout << temp << endl;  
  50. }  


对于类模版,成员函数的声明必须在.h文件中完成,.cpp中不写code。否则编译器会报错:无法解析的外部符号 "public: void __thiscall Student<int,int>::add(int,int)" ,该符号在函数 _wmain 中被引用

另外对于类成员函数的定义要跟随其声明,如果在类外进行函数的定义需要添加模版的声明。见如下程序:

[cpp] view plain copy
  1. #pragma once  
  2. template<class T1, class T2>  
  3. class Student  
  4. {  
  5.  /*在声明的同时进行定义,在这里可以看出,编译器在编译的时候, 
  6.  先对成员变量进行编译,然后在编译成员函数*/  
  7. public:  
  8.  Student(void){m_TNum1 = 0;  
  9.  m_TNum2 = 0;  
  10.  };        
  11.  void add(T1 t_num1, T2 t_num2);  
  12.  ~Student(void){};  
  13. public:  
  14.  T1 m_TNum1;  
  15.  T2 m_TNum2;  
  16. };  
  17. // 类外进行声明  
  18. template<class T1, class T2>  
  19. void Student<T1, T2>::add(T1 t_num1, T2 t_num2)  
  20. {  
  21.  T1 temp = 0;  
  22.  temp = t_num1 + t_num2;  
  23.  cout << temp <<endl;  

你可能感兴趣的:(类模版和函数模版需要注意的)