使用vue2+axios+chart.js画折线图 ,出现 RangeError: Maximum call stack size exceeded 错误

目录

 

效果图

解决方案

修正要点


效果图

修改前App.vue代码:

<template><div id="app"><canvas id="myChart"></canvas></div>
</template><script>
import axios from 'axios';
import { Chart, registerables } from 'chart.js';
import 'chartjs-adapter-luxon';Chart.register(...registerables);export default {name: 'App',data() {return {chart: null,remainingData: [],speedData: []};},mounted() {this.fetchData();setInterval(this.fetchData, 60000);  // 每分钟更新数据},methods: {fetchData() {axios.get('/data.json').then(response => {console.log('接收数据:', response.data);// 确保转换后的时间戳格式正确,使用 new Date(item.timestamp) 而不是 Date(item.timestamp)this.remainingData = response.data.map(item => {return { x: new Date(item.timestamp), y: item.remaining };});this.calculateSpeed();// 确保remainingData和speedData不为空,防止图例点击时出现undefined错误if (this.remainingData.length === 0) {this.remainingData = [{ x: new Date(), y: 0 }];}if (this.speedData.length === 0) {this.speedData = [{ x: new Date(), y: 0 }];}// 防止无限递归调用,只在数据确实有更新时更新图表if (!this.chart) {this.updateChart();} else {// 更新数据并重新渲染this.chart.data.datasets[0].data = this.remainingData;this.chart.data.datasets[1].data = this.speedData;this.chart.update();  // 只更新已有的图表}}).catch(error => {console.log('获取数据异常:', error);});},calculateSpeed() {const speeds = [];for (let i = 1; i < this.remainingData.length; i++) {const speed = this.remainingData[i].y - this.remainingData[i - 1].y;speeds.push({ x: this.remainingData[i].x, y: speed });}this.speedData = speeds;},updateChart() {this.chart = new Chart(document.getElementById('myChart').getContext('2d'),{type: 'line',data: {datasets: [{label: 'Remaining',data: this.remainingData,borderColor: 'rgb(75, 192, 192)',tension: 0.1,fill: false},{label: 'Speed',data: this.speedData,borderColor: 'rgb(255, 99, 132)',tension: 0.1,fill: false}]},options: {scales: {x: {type: 'time',  // 使用时间轴time: {unit: 'minute',  // 设为 minute 使点不聚集tooltipFormat: 'yyyy-MM-dd HH:mm',displayFormats: {minute: 'HH:mm'  // X 轴时间格式化}},ticks: {source: 'auto',autoSkip: true,maxRotation: 0,  // 防止时间戳旋转}},y: {beginAtZero: true  // 保证 Y 轴从 0 开始}},plugins: {tooltip: {mode: 'index',intersect: false},legend: {onClick: (e, legendItem, legend) => {const datasetIndex = legendItem.datasetIndex;const dataset = legend.chart.data.datasets[datasetIndex];// 确保点击图例时,数据集存在并且被初始化if (!dataset || !dataset.data || !Array.isArray(dataset.data)) {console.log('数据集不存在或者未初始化');return;}// 切换数据集的显示状态dataset.hidden = !dataset.hidden;legend.chart.update();}}},responsive: true,maintainAspectRatio: false}});}}
}
</script>

出现 RangeError: Maximum call stack size exceeded 错误,可能是由于 setInterval 直接引用了 fetchData,而 fetchData 中又更新了图表或页面元素,导致无限递归问题。每次调用 fetchData 可能会触发多次图表更新,最终导致栈溢出。

为了解决这个问题,你可以通过以下方式优化:

解决方案

  1. 使用箭头函数传递上下文:避免 setInterval 中的上下文丢失,确保 this 的正确性。
  2. 防止重复图表创建:在 fetchDataupdateChart 中,确保图表只在需要的时候更新,避免重复调用造成栈溢出。
  3. 处理异步调用:确保每次 fetchData 的数据处理逻辑正确,避免多个请求导致的图表过度更新。

修改后的App..vue 代码:

<template><div id="app"><canvas id="myChart"></canvas></div>
</template><script>
import axios from 'axios';
import { Chart, registerables } from 'chart.js';
import 'chartjs-adapter-luxon';Chart.register(...registerables);export default {name: 'App',data() {return {chart: null,remainingData: [],speedData: [],lastFetchedData: null,  // 存储上次的数据intervalId: null  // 存储 setInterval 的 ID};},mounted() {this.fetchData();this.intervalId = setInterval(this.fetchData, 60000);  // 每分钟更新数据},beforeUnmount() {// 清理定时器,防止组件销毁时继续触发if (this.intervalId) {clearInterval(this.intervalId);}},methods: {fetchData() {axios.get('/data.json').then(response => {console.log('接收数据:', response.data);const newData = response.data.map(item => {return { x: new Date(item.timestamp), y: item.remaining };});// 判断数据是否真的有变化,只有在变化时才更新图表if (JSON.stringify(newData) !== JSON.stringify(this.remainingData)) {console.log('数据发生变化,更新图表');this.remainingData = newData;this.calculateSpeed();// 确保remainingData和speedData不为空if (this.remainingData.length === 0) {this.remainingData = [{ x: new Date(), y: 0 }];}if (this.speedData.length === 0) {this.speedData = [{ x: new Date(), y: 0 }];}// 防止无限递归调用,只在数据确实有更新时更新图表if (!this.chart) {this.updateChart();} else {this.chart.data.datasets[0].data = this.remainingData;this.chart.data.datasets[1].data = this.speedData;this.chart.update();  // 只更新已有的图表}} else {console.log('数据未变化,不更新图表');}}).catch(error => {console.log('获取数据异常:', error);});},calculateSpeed() {const speeds = [];for (let i = 1; i < this.remainingData.length; i++) {const speed = this.remainingData[i].y - this.remainingData[i - 1].y;speeds.push({ x: this.remainingData[i].x, y: speed });}this.speedData = speeds;},updateChart() {this.chart = new Chart(document.getElementById('myChart').getContext('2d'),{type: 'line',data: {datasets: [{label: 'Remaining',data: this.remainingData,borderColor: 'rgb(75, 192, 192)',tension: 0.1,fill: false},{label: 'Speed',data: this.speedData,borderColor: 'rgb(255, 99, 132)',tension: 0.1,fill: false}]},options: {scales: {x: {type: 'time',time: {unit: 'minute',tooltipFormat: 'yyyy-MM-dd HH:mm',displayFormats: {minute: 'HH:mm'}},ticks: {source: 'auto',autoSkip: true,maxRotation: 0,}},y: {beginAtZero: true}},plugins: {tooltip: {mode: 'index',intersect: false},legend: {onClick: (e, legendItem, legend) => {const datasetIndex = legendItem.datasetIndex;const dataset = legend.chart.data.datasets[datasetIndex];if (!dataset || !dataset.data || !Array.isArray(dataset.data)) {console.log('数据集不存在或者未初始化');return;}dataset.hidden = !dataset.hidden;legend.chart.update();}}},responsive: true,maintainAspectRatio: false}});}}
}
</script>

data.json:

[{"remaining": 32.76,"timestamp": "2024-09-10 11:51:19"},{"remaining": 32.78,"timestamp": "2024-09-10 11:50:14"},{"remaining": 32.81,"timestamp": "2024-09-10 11:49:11"},{"remaining": 32.85,"timestamp": "2024-09-10 11:48:06"},{"remaining": 32.88,"timestamp": "2024-09-10 11:47:02"},{"remaining": 32.9,"timestamp": "2024-09-10 11:45:57"},{"remaining": 32.93,"timestamp": "2024-09-10 11:44:53"},{"remaining": 32.95,"timestamp": "2024-09-10 11:43:49"},{"remaining": 32.98,"timestamp": "2024-09-10 11:42:46"},{"remaining": 33.0,"timestamp": "2024-09-10 11:41:41"},{"remaining": 33.03,"timestamp": "2024-09-10 11:40:36"},{"remaining": 33.05,"timestamp": "2024-09-10 11:39:32"},{"remaining": 33.08,"timestamp": "2024-09-10 11:38:27"},{"remaining": 33.1,"timestamp": "2024-09-10 11:37:22"},{"remaining": 33.13,"timestamp": "2024-09-10 11:36:17"},{"remaining": 33.15,"timestamp": "2024-09-10 11:35:12"},{"remaining": 33.2,"timestamp": "2024-09-10 11:34:07"},{"remaining": 33.22,"timestamp": "2024-09-10 11:33:02"},{"remaining": 33.24,"timestamp": "2024-09-10 11:31:57"},{"remaining": 33.26,"timestamp": "2024-09-10 11:30:52"},{"remaining": 33.29,"timestamp": "2024-09-10 11:29:47"},{"remaining": 33.31,"timestamp": "2024-09-10 11:28:42"},{"remaining": 33.33,"timestamp": "2024-09-10 11:27:37"},{"remaining": 33.36,"timestamp": "2024-09-10 11:26:32"},{"remaining": 33.38,"timestamp": "2024-09-10 11:25:28"},{"remaining": 33.4,"timestamp": "2024-09-10 11:24:23"},{"remaining": 33.43,"timestamp": "2024-09-10 11:23:18"},{"remaining": 33.45,"timestamp": "2024-09-10 11:22:15"},{"remaining": 33.47,"timestamp": "2024-09-10 11:21:12"},{"remaining": 33.52,"timestamp": "2024-09-10 11:20:07"},{"remaining": 33.54,"timestamp": "2024-09-10 11:19:04"},{"remaining": 33.56,"timestamp": "2024-09-10 11:18:02"},{"remaining": 33.57,"timestamp": "2024-09-10 11:16:57"},{"remaining": 33.57,"timestamp": "2024-09-10 11:15:52"},{"remaining": 33.58,"timestamp": "2024-09-10 11:14:47"},{"remaining": 33.59,"timestamp": "2024-09-10 11:13:42"},{"remaining": 33.6,"timestamp": "2024-09-10 11:12:39"},{"remaining": 33.61,"timestamp": "2024-09-10 11:11:34"},{"remaining": 33.62,"timestamp": "2024-09-10 11:10:31"},{"remaining": 33.62,"timestamp": "2024-09-10 11:09:26"},{"remaining": 33.63,"timestamp": "2024-09-10 11:08:21"},{"remaining": 33.64,"timestamp": "2024-09-10 11:07:16"},{"remaining": 33.65,"timestamp": "2024-09-10 11:06:12"},{"remaining": 33.66,"timestamp": "2024-09-10 11:05:07"},{"remaining": 33.67,"timestamp": "2024-09-10 11:04:04"},{"remaining": 33.68,"timestamp": "2024-09-10 11:03:00"},{"remaining": 33.69,"timestamp": "2024-09-10 11:01:55"},{"remaining": 33.7,"timestamp": "2024-09-10 11:00:50"},{"remaining": 33.71,"timestamp": "2024-09-10 10:59:46"},{"remaining": 33.72,"timestamp": "2024-09-10 10:58:41"},{"remaining": 33.72,"timestamp": "2024-09-10 10:57:36"},{"remaining": 33.73,"timestamp": "2024-09-10 10:56:33"},{"remaining": 33.74,"timestamp": "2024-09-10 10:55:30"},{"remaining": 33.75,"timestamp": "2024-09-10 10:54:28"},{"remaining": 33.75,"timestamp": "2024-09-10 10:53:25"},{"remaining": 33.76,"timestamp": "2024-09-10 10:52:20"},{"remaining": 33.76,"timestamp": "2024-09-10 10:51:15"},{"remaining": 33.77,"timestamp": "2024-09-10 10:50:11"},{"remaining": 33.78,"timestamp": "2024-09-10 10:49:06"},{"remaining": 33.78,"timestamp": "2024-09-10 10:48:01"},{"remaining": 33.79,"timestamp": "2024-09-10 10:46:56"},{"remaining": 33.79,"timestamp": "2024-09-10 10:45:51"},{"remaining": 33.8,"timestamp": "2024-09-10 10:44:45"},{"remaining": 33.81,"timestamp": "2024-09-10 10:43:40"},{"remaining": 33.81,"timestamp": "2024-09-10 10:42:35"},{"remaining": 33.82,"timestamp": "2024-09-10 10:41:32"},{"remaining": 33.82,"timestamp": "2024-09-10 10:40:27"},{"remaining": 33.83,"timestamp": "2024-09-10 10:39:24"},{"remaining": 33.83,"timestamp": "2024-09-10 10:38:56"},{"remaining": 34.83,"timestamp": "2024-09-10 02:12:51"},{"remaining": 34.83,"timestamp": "2024-09-10 02:11:44"},{"remaining": 34.84,"timestamp": "2024-09-10 02:10:38"},{"remaining": 34.84,"timestamp": "2024-09-10 02:09:31"},{"remaining": 34.84,"timestamp": "2024-09-10 02:08:28"},{"remaining": 34.85,"timestamp": "2024-09-10 02:07:21"},{"remaining": 34.85,"timestamp": "2024-09-10 02:06:16"},{"remaining": 34.86,"timestamp": "2024-09-10 02:05:11"},{"remaining": 34.86,"timestamp": "2024-09-10 02:04:06"},{"remaining": 34.87,"timestamp": "2024-09-10 02:03:03"},{"remaining": 34.87,"timestamp": "2024-09-10 02:01:58"},{"remaining": 34.87,"timestamp": "2024-09-10 02:00:52"},{"remaining": 34.88,"timestamp": "2024-09-10 01:59:48"},{"remaining": 34.88,"timestamp": "2024-09-10 01:58:43"},{"remaining": 34.89,"timestamp": "2024-09-10 01:57:40"},{"remaining": 34.89,"timestamp": "2024-09-10 01:56:35"},{"remaining": 34.9,"timestamp": "2024-09-10 01:55:30"},{"remaining": 34.92,"timestamp": "2024-09-10 01:54:27"},{"remaining": 34.94,"timestamp": "2024-09-10 01:53:22"},{"remaining": 34.96,"timestamp": "2024-09-10 01:52:18"},{"remaining": 34.98,"timestamp": "2024-09-10 01:51:15"},{"remaining": 35.01,"timestamp": "2024-09-10 01:50:09"},{"remaining": 35.03,"timestamp": "2024-09-10 01:49:02"},{"remaining": 35.05,"timestamp": "2024-09-10 01:47:59"},{"remaining": 35.05,"timestamp": "2024-09-10 01:46:55"},{"remaining": 35.05,"timestamp": "2024-09-10 01:45:50"},{"remaining": 35.06,"timestamp": "2024-09-10 01:44:46"},{"remaining": 35.08,"timestamp": "2024-09-10 01:43:41"},{"remaining": 35.1,"timestamp": "2024-09-10 01:42:38"},{"remaining": 35.12,"timestamp": "2024-09-10 01:41:34"},{"remaining": 35.14,"timestamp": "2024-09-10 01:40:31"},{"remaining": 35.15,"timestamp": "2024-09-10 01:39:26"},{"remaining": 35.17,"timestamp": "2024-09-10 01:38:21"},{"remaining": 35.19,"timestamp": "2024-09-10 01:37:18"},{"remaining": 35.21,"timestamp": "2024-09-10 01:36:13"},{"remaining": 35.22,"timestamp": "2024-09-10 01:35:10"},{"remaining": 35.22,"timestamp": "2024-09-10 01:34:05"},{"remaining": 35.26,"timestamp": "2024-09-10 01:31:29"},{"remaining": 35.28,"timestamp": "2024-09-10 01:30:24"},{"remaining": 35.3,"timestamp": "2024-09-10 01:29:21"},{"remaining": 35.31,"timestamp": "2024-09-10 01:28:26"},{"remaining": 35.33,"timestamp": "2024-09-10 01:27:24"},{"remaining": 35.35,"timestamp": "2024-09-10 01:26:19"},{"remaining": 35.37,"timestamp": "2024-09-10 01:25:13"},{"remaining": 35.38,"timestamp": "2024-09-10 01:24:09"},{"remaining": 35.39,"timestamp": "2024-09-10 01:23:03"},{"remaining": 35.41,"timestamp": "2024-09-10 01:21:58"},{"remaining": 35.43,"timestamp": "2024-09-10 01:20:53"},{"remaining": 35.47,"timestamp": "2024-09-10 01:19:06"},{"remaining": 35.49,"timestamp": "2024-09-10 01:18:01"}
]

修正要点

  1. 使用箭头函数在 setInterval 中绑定正确的 this 上下文

    • 使用 setInterval(() => { this.fetchData(); }, 60000),确保 fetchData 在每次调用时使用正确的组件上下文,避免函数指针错误。
  2. 防止重复创建图表

    • 检查 this.chart 是否已存在,只有在首次创建时调用 updateChart,在后续更新中只调用 this.chart.update(),防止不断创建新图表导致的栈溢出。
  3. 优化 fetchData 的数据处理逻辑

    • 确保每次数据更新只更新已有图表,不会触发多余的重绘或递归调用。

通过以上优化,可以避免无限递归和重复调用引发的 Maximum call stack size exceeded 错误。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/423732.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

QT多线程编程(基础概念以及示例)

QT多线程编程 前言&#xff1a;基础夯实&#xff1a;一:多线程概述二:QT多线程的使用1. 继承QThread类2. 继承QObject类3. QtConcurrent模块 三:线程同步与通信四:线程安全五:线程管理六:总结 效果展示&#xff1a;实现功能&#xff1a;核心代码&#xff1a;mainwindow.hmythre…

k8s 部署 ruoyi 前后端分离项目

本文视频版: https://www.bilibili.com/video/BV17ugkePEeN 参考 https://blog.csdn.net/qq_50247813/article/details/136934090 https://gitee.com/nasaa/RuoYi-Vue-cloud https://www.itsgeekhead.com/tuts/kubernetes-129-ubuntu-22-04-3/ https://kubernetes.io/docs…

【信号】SIGCHLD信号--了解

SIGCHLD是多少号信号呢&#xff1f;17号 我们知道用wait和waitpid函数清理僵尸进程,父进程可以阻塞等待子进程结束,也可以非阻塞地查询是否有子进程结束等待清理(也就是轮询的方式)。采用第一种方式,父进程阻塞了就不能处理自己的工作了;采用第二种方式,父进程在处理自己的工作…

Leetcode 二叉树中根遍历

采用递归算法&#xff0c;并且用一个向量来存储节点值。 算法C代码如下&#xff1a; /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}*…

Python库Plotly学习笔记

Plotly是一个用于创建交互式图形的Python库。它提供了丰富的图表类型&#xff0c;包括散点图、线图、柱状图、热力图、饼图等&#xff0c;以及高级的可视化功能&#xff0c;如动画、交互、数据缩放和拖动等。 Plotly库的主要特点&#xff1a; 交互式图表&#xff1a;Plotly创…

git 更新LingDongGui问题解决

今天重新更新灵动gui的代码&#xff0c;以便使用最新的arm-2d&#xff0c;本来以为是比较简单的一件事情&#xff08;因为以前已经更新过一次&#xff09;&#xff0c;却搞了大半天&#xff0c;折腾不易啊&#xff0c;简单记录下来&#xff0c;有同样遇到问题的同学参考&#x…

SSM框架学习(三、MyBatis实践:提高持久层数据处理效率)

目录 一、Mybatis简介 1.简介 2.持久层框架对比 3.快速入门&#xff08;基于Mybatis3方式&#xff09; 4.ibatis方式的实现和原理 5.ibatis与mybatis之间的关系 二、Mybatis基本使用 1.向 sql 语句传参 &#xff08;1&#xff09;mybatis日志输出配置 &#xff08;2&…

存储课程学习笔记5_iouring的练习(io_uring,rust_echo_bench,fio)

我们知道&#xff0c;在处理大量高并发网络时&#xff0c;一般考虑并发&#xff0c;以及设计对应的方案&#xff08;比如select,poll,epoll&#xff09;等。 那么如果频繁进行文件或者磁盘的操作&#xff0c;如何考虑性能和并发&#xff0c;这里就可以考虑用到io_uring。 0&a…

RK3588镜像打包制作,替换文件系统

1.在开发板上安装async apt-get async 2.在另一台linux机器上执行命令拷贝文件系统 注意&#xff1a; 这里使用root权限或者账户 mkdir rootfs rsync -avx root192.168.1.3:/ rootfs 3.制作空镜像文件 先去开发板上验证自己的系统使用了多少空间&#xff0c;然后输入命令制…

rancker 图形化界面

rancker 图形化界面 图形化界面进行k8s集群的管理 rancher自带监控————普罗米修斯 #在master和两个node上都操作 [rootmaster01 opt]# rz -E rz waiting to receive. [rootmaster01 opt]# docker load -i rancher.tar ​ #在master上操作 [rootmaster01 opt]# docker pul…

828华为云征文|华为云Flexus X搭建借贷管理系统、二次开发借贷小程序 前端源码uniapp

在华为云828 B2B企业节的盛宴中&#xff0c;Flexus X实例以其卓越的算力性能和灵活的资源配置脱颖而出。对于追求极致性能、渴望在借贷管理、电商交易等场景中脱颖而出的您来说&#xff0c;Flexus X无疑是最佳拍档。搭载创新加速引擎&#xff0c;让您的自建MySQL、Redis、Nginx…

浙大数据结构:04-树6 Complete Binary Search Tree

这道题利用了完全二叉树的性质&#xff0c;我也参考了一些代码写的。 &#xff08;自己一开始写了别的方法&#xff0c;但一直过不了最后一个测试点&#xff0c;红温了&#xff09; 机翻&#xff1a; 1、条件准备 用vector存输入的数据&#xff0c;另一个数组存输出的结果&a…

实战外网配置——光猫桥接+路由器PPPoE拨号+防火墙外网链路健康检查+外网流量负载均衡

一、适用场景&#xff1a; 1、企业规模较大时&#xff0c;1条公网带宽流量可能不足&#xff0c;需要用到多条公网出口时。 2、企业有业务需要静态ip映射&#xff0c;但是因静态ip专线价格较高&#xff0c;所以需要拨号光纤承载较多的下行流量。 3、当公网出口有多条链路&#…

学习笔记 - 知识图谱的符号表示方法

学习笔记 - 知识图谱的符号表示方法 说明&#xff1a; 首次发表日期&#xff1a;2024-09-13个人阅读学习并摘录成笔记 知识表示的相关名词定义 以下内容摘录自 Knowledge Graphs Applied 2.3小节&#xff0c;然后AI翻译人工润色。 实体&#xff08;Entities&#xff09;—表…

Python | 练习作业 2

为学生登录系统新增搜索功能。 第二天作业的解题思路&#xff1a; # 1.创建一个空列表保存搜索结果 # 2.让用户输入要搜索的内容 # 3.遍历学生信息&#xff0c;检查学生的id name age gender score # 中的属性值 是否跟用户搜索的内容一致 # 4.如果有一致的属性 那么就将该学生…

TikTok运营需要的独立IP如何获取?

TikTok作为当下炙手可热的社交媒体平台&#xff0c;吸引了众多个人创作者和企业进驻。在进行TikTok运营时&#xff0c;许多经验丰富的用户都倾向于选择独立IP。那么&#xff0c;TikTok运营为什么需要独立IP&#xff1f;又该如何获取呢&#xff1f;本文将详细为您解答这些问题。…

vue2基础系列教程之v-model及面试高频问题

v-model是表单组件里面的核心知识点&#xff0c;这个指令给我们写表单业务带来了很大的方便。 元素标签上的 v-model 指令用于双向绑定数据,它是一个语法糖&#xff0c;可以用于代替 v-bind:value 和 input 例如&#xff1a;<input v-model"message" placeholder…

Springboot中mybatis的使用

一.创建Springboot项目并加载依赖 1.利用IDEA创建SpringBoot项目&#xff0c;并勾选必须依赖&#xff0c;步骤如下&#xff08;IDEA版本为2024版&#xff09; 注意&#xff1a; 1.首先更换镜像源&#xff0c;否则加载配置环境比较慢&#xff0c;网上搜阿里的镜像源就行。 2…

Python数据类型详解:这12个类型你都知道吗?

在Python中&#xff0c;数据类型是编程的基石&#xff0c;它们定义了可以操作的数据的种类。Python是一种动态类型语言&#xff0c;意味着你不需要显式地声明变量的类型&#xff1b;Python解释器会自动推断出变量所存储数据的类型。Python提供了多种内置数据类型&#xff0c;这…

c++类和对象(3):默认成员函数(下)

1.拷贝构造函数 如果⼀个构造函数的第⼀个参数是自身类类型的引用&#xff0c;且任何额外的参数都有默认值&#xff0c;则此构造函数也叫做拷贝构造函数&#xff0c;也就是说拷贝构造是⼀个特殊的构造函数。 c规定&#xff1a;类类型的传值传参必须用拷贝构造 1.1拷贝构造函数…