前言:自从开始学模板了后,小编在练习的过程中。常常一编译之后出现几十个错误,而且还是那种看都看不懂那种(此刻只想一句MMP)。于是写了便写了类模板友元函数的用法这篇博客。来记录一下自己的学习。

普通友元函数的写法:

第一种:(直接上代码吧)

#include <iostream>
#include <string> using namespace std; template<class T>
class Person{
public:
Person(T n)
{
cout << "Person" << endl;
this->name = n;
}
~Person()
{
cout << "析构函数" << endl;
}
//友元函数
/********************************/
template<class T>
friend void print(Person<T> &p);
/*******************************/
private:
T name;
}; //友元函数
template<class T>
void print(Person<T> &p)
{
cout << p.name << endl;
} int main()
{
Person<string>P("XiaoMing");
print(P); system("pause");
return ;
}

第二种方法:

 #include <iostream>
#include <string> using namespace std; //方法二必不可缺的一部分
/**************************************/
template<class T> class Person;
template<class T> void print(Person<T> &p);
/****************************************/
template<class T>
class Person{
public:
Person(T n)
{
cout << "Person" << endl;
this->name = n;
}
~Person()
{
cout << "析构函数" << endl;
}
//友元函数
/********************************/
friend void print<T>(Person<T> &p);
/*******************************/
private:
T name;
}; //友元函数
template<class T>
void print(Person<T> &p)
{
cout << p.name << endl;
} int main()
{
Person<string>P("XiaoMing");
print(P); system("pause");
return ;
}

运算符重载中的友元函数:

方法一:

#include <iostream>
#include <string> using namespace std; template<class T>
class Person{
public:
Person(T n)
{
cout << "Person" << endl;
this->name = n;
}
~Person()
{
cout << "析构函数" << endl;
}
//友元函数
/********************************/
template<class T>
friend ostream& operator<<(ostream &os, Person<T> &p);
/*******************************/
private:
T name;
}; //运算符重载
template<class T>
ostream& operator<<(ostream &os, Person<T> &p)
{
os << p.name << endl;
return os;
} int main()
{
Person<string>P("XiaoMing");
cout << P << endl;
system("pause");
return ;
}

方法二:

#include <iostream>
#include <string> using namespace std; template<class T>
class Person{
public:
Person(T n)
{
cout << "Person" << endl;
this->name = n;
}
~Person()
{
cout << "析构函数" << endl;
}
//友元函数
/********************************/
//template<class T>
friend ostream& operator<<<T>(ostream &os, Person<T> &p);
/*******************************/
private:
T name;
}; //运算符重载
template<class T>
ostream& operator<<(ostream &os, Person<T> &p)
{
os << p.name << endl;
return os;
} int main()
{
Person<string>P("XiaoMing");
cout << P << endl;
system("pause");
return ;
}

最新文章

  1. SQL Tuning 基础概述02 - Explain plan的使用
  2. Oracle中已有数据的字段类型修改
  3. Linux与Windows xp操作系统启动过程
  4. View Controller Relationships
  5. Java BigDecimal使用
  6. centos7安装python-pip
  7. 高级UNIX环境编程4 文件和目录
  8. if语句之有房么?有钱么?有能力么?
  9. 如何解决 chrome 58 版本更新导致的 fiddler https 抓包不可用问题
  10. 初始Spring MVC——练手小项目
  11. hadoop集群崩溃,因为tmp下/tmp/hadoop-hadoop/dfs/name文件误删除
  12. @vue-cli3创建项目报错:ERROR command failed: npm install --loglevel error --registry=https://registry.npm.taobao.org --di
  13. xddpay.com 个人支付接口接入流程
  14. 基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件
  15. Java高并发情况下的锁机制优化
  16. 深入学习webpack
  17. Tensorflow问题汇总
  18. 万恶之源 - Python文件操作
  19. P3870 [TJOI2009]开关
  20. 工作之路---记录LZ如何在两年半的时间内升为PM

热门文章

  1. Flutter学习之制作底部菜单导航
  2. RESULT_OK,RESULT_CANCELED,RESULT_FIRST_USER
  3. 润乾V5手机报表说明文档
  4. ASP.NET MVC 实现区域 项目分离 (比较好的方式)
  5. 相关与卷积(数字信号处理)的数学原理及 Python 实现
  6. Vue2学习笔记:html属性
  7. 在asp.net一般应用程序中使用session
  8. 转:tomcat安全设置
  9. [翻译] NSRegexTester
  10. SparkSql实现Mysql到hive的数据流动