在学习类模板成员函数类外实现遇到了三个错误,水平有限,请大家帮我解决一下
#include
using namespace std;
#include
template<class T1,class T2>
class Person
{
public:
Person(T1 name,T2 age);
T1 m_Name;
T2 m_Age;
};
//构造函数类外实现
template<class T1,class T2>
Person::Person(T1 name,T2 age)
{
this->m_Name=name;
this->m_Age=age;
}
template<class T1,class T2>
void Person::showPerson()
{
cout<<"name:"<<this->m_Name<<"age:"<<this->m_Age<void test01()
{
Personint > P("Tom",20);
P.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}
三个错误原因是
错误 1 error C2143: 语法错误 : 缺少“,”(在“.”的前面)
错误 2 error C2649: “typename”: 不是“class”
错误 3 fatal error C1004: 发现意外的文件尾
#include <iostream>
using namespace std;
#include <string>
template <class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age);
T1 m_Name;
T2 m_Age;
void showPerson();
};
// 构造函数类外实现
template <class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
template <class T1, class T2>
void Person<T1, T2>::showPerson()
{
cout << "name:" << this->m_Name << "age:" << this->m_Age << endl;
}
void test01()
{
Person<string, int> P("Tom", 20);
P.showPerson();
}
int main()
{
test01();
system("pause");
return 0;
}