1.类模板的使用方法:
//这里要加上模板声明
template <class T1,class T2> class people{ private: T1 name; T2 age; public: people(T1 a,T2 b); void say(); };
//这里要加上模板声明 template <class T1,class T2> people<T1,T2>:: people(T1 a,T2 b):name(a),age(b){ cout<<"this is constructor"<<endl; }
//这里要加上模板声明 template <class T1,class T2> void people<T1,T2>::say(){ cout<<"my name is: "<<name <<",my age is: "<<age<<endl; cout<<typeid(name).name()<<" "<<typeid(age).name()<<endl; } int main(){ //跟普通对象创建一样,只是在类前面声明模板类型 people<string,int> p1("liuweiwei",10); p1.say(); people<int,int> p2(100,23); p2.say(); return 0; }