类模板成员函数类外实现

  1. class Person {
  2. public:
  3. Person(T1 name, T2 age);
  4. void showPerson() {
  5. cout << "name: " << this->m_Name << " age: " << this->m_Age << endl;
  6. }
  7. T1 m_Name;
  8. T2 m_Age;
  9. };
  10. template <class T1, class T2>
  11. Person<T1,T2>::Person(T1 name, T2 age) {//<表示Person类模板的实现>
  12. this->m_Age = age;
  13. this->m_Name = name;
  14. }
  15. template <class T1, class T2>
  16. void Person<T1,T2>::showPerson(){
  17. //实现
  18. }

在类内只声明,不实现,在外部实现需要加模板和模板的作用域