#include <iostream>
#include <string>
//注意头文件:string 和 string.h 的区别;
using namespace std;
//类模板与友元函数---友元函数类外实现:
template<class T1,class T2>
class Person
{
//友元函数类外实现:
friend void printPerson(Person<T1, T2>& p);
/*
{
cout << "姓名:" << p.m_Name << " 年龄: " << p.m_Age << endl;
}
*/
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
private:
T1 m_Name;
T2 m_Age;
};
//类外实现:
template<class T1, class T2>
void printPerson(Person<T1, T2>& p)
{
cout << "姓名:" << p.m_Name << " 年龄: " << p.m_Age << endl;
}
void test01()
{
Person<string, int>p1("asadasd", 10);
//printPerson(p1);
}
//无法解析----链接错误;
//通过注释printPerson(),能成功生成,发现是这句话出了问题;
//对于printPerson()是一个普通函数,而对
C++------类模板友元函数类外实现
最新推荐文章于 2023-06-24 17:58:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)