c++之类模板成员函数的类外实现

#include<iostream>
using namespace std;

template<class T1,class T2>
class Person {
public:
    Person(T1 name,T2 age);
    void show();
    T1 name;
    T2 age;
};

template<class T1,class T2>
Person<T1,T2>::Person(T1 name, T2 age) {
    this->name = name;
    this->age = age;
}
//对于成员函数,需要指明类的参数的代表
template<class T1, class T2>
void Person<T1, T2>::show() {
    cout << this->name << endl;
    cout << this->age << endl;
}

void test() {
    Person<string, int> p("tom",12);
    p.show();
}

int main() {
    test();
    system("pause");
    return 0;
}

输出:

原文地址:https://www.cnblogs.com/xiximayou/p/12107699.html