C++之error: cannot bind non-const lvalue reference of type ‘myString’ to an rvalue of type ‘myString
先看代码(不想看代码可以直接看代码后的问题描述)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
这是一个字符串类,问题出现在了两个符号重载,()和=
()重载是想对字符串对象做一个切片,返回一个临时对象,=重载就不用说了,就是赋值。
问题就出现在总是无法将这个切片后的临时对象赋值给等号前的对象,编译后如下:
在网上一番查找后找到一个类似问题
https://blog.csdn.net/u011068702/article/details/64443949
也就是说,在st1(0,4)时存在了一个临时变量,当把这个临时变量传给st时,由于在=重载函数声明中,参数为myString&,而并不是常量引用。
这个错误是C++编译器的一个关于语义的限制。
如果一个参数是以非const引用传入,c++编译器就有理由认为程序员会在函数中修改这个值,并且这个被修改的引用在函数返回后要发挥作用。但如果你把一个临时变量当作非const引用参数传进来,由于临时变量的特殊性,程序员并不能操作临时变量,而且临时变量随时可能被释放掉,所以,一般说来,修改一个临时变量是毫无意义的,据此,c++编译器加入了临时变量不能作为非const引用的这个语义限制。
了解这个语义以后就简单了,只需给=重载参数加上const常量限制符。
(类中=重载函数声明也别忘了要加上const)
加上以后程序的运行结果
可以,很正确。
总结:
c++中临时变量不能作为非const的引用参数
2019/12/14更新
最近看到一个类似问题,即C++11
1 2 3 |
|
我们知道前++就是直接对对象自增后返回对象,而后++会先返回记录当前值,在自增,最后返回一个无名的临时对象,那么 i++++就是让第一个后++返回的无名临时对象再自增,这样对C++是无意义的,所以这样就无法编译通过。//ps.这就是常说的 举一隅以三隅反 吧
不积小流无以成江河
C++之error: cannot bind non-const lvalue reference of type ‘myString’ to an rvalue of type ‘myString相关推荐
- QT之error: cannot bind non-const lvalue reference of type ‘CBaowen’ to an rvalue of type ‘CBaowen
问题描述:QT中自己编写了一个结构体变量CBaowen,报文中含有函数重载部分(如下所示), 之后定义一个队列变量 QQueue<CBaowen> queue; 当给队列变量赋值时que ...
- cannot bind non-const lvalue reference of type ‘xxx‘ to an rvalue of type ‘xxx‘
文章目录 报错代码 报错 原因 解决方案 报错代码 #include<iostream> using namespace std; class Complex{private:double ...
- C++ stack pop()返回值 error: cannot initialize a variable of type ‘int‘ with an rvalue of type ‘void‘
LeetCode一道链表倒置题目 struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {} };c ...
- 【报错】关于[Error] cannot bind non-const lvalue reference of type ‘std::String‘ to an rvalue……的一个解决方案
使用dev c++进行一波教科书代码的练习时出现了一点问题,于是来记录一波. 报错信息如下: [Error] cannot bind non-const lvalue reference of typ ...
- 【C++---const引用】数组进行指针引用传递给函数error: non-const lvalue reference of type ‘int*‘ to an rvalue
目录 原因 类型转换 手动转换 自动转换 关于临时量 关于常量引用(const的引用) const引用的对象不是不能被修改了吗,这里为什么被修改了? 不是说是const引用吗?为什么又变成了引用所绑定 ...
- cannot bind non-const lvalue reference of type ‘***‘ to an rvalue of type ‘***‘解决方法
在写C++作业的时候,我发现使用 #include <iostream> class complex{public:int real;int imaginary;public:comple ...
- error: non-const lvalue reference to type cannot bind to a value of unrelated type
项目场景: 在项目代码编译中报错信息如下: error: non-const lvalue reference to type '...' cannot bind to a value of unre ...
- 关于C++的cannot bind non-const lvalue reference of type...问题
关于C++的cannot bind non-const lvalue reference of type-问题 先看下面的代码,一个很简单的切分字符串并输出的函数. #include <iost ...
- [EAI ERROR]: Cannot bind to the specified serial port /dev/ttyUSB0. process has died[pid 108767, ex
运行激光雷达出现错误 [EAI INFO]: Try to connect the port /dev/ttyUSB0 again after 2 s . [EAI INFO]: Try to con ...
最新文章
- 2007年你必须学习的10项.NET技术
- 利用js对页面数据进行排序
- Oracle 触发器的使用小结
- Python Django Cookie的设置和获取相关属性
- Flagger on ASM——基于Mixerless Telemetry实现渐进式灰度发布系列 1 遥测数据
- 基于vue的nuxt框架cnode社区服务端渲染
- 自动化部署工具Fabric简介
- 【Java】JavaSocket编程开发聊天室-服务器端部分
- journalctl命令详解,与如何查看系统日志
- 【CVPR2022】论文列表与下载——PartFour
- iOS和安卓的base64
- 人工智能 AI 绘画发展史
- czy的后宫——矩阵快速幂优化DP
- 递归算法(练习习题)
- [转载备用]微信直播的优势及微信直播搭建过程(点赞:主播妹子有点靓哦)
- 马上:Android pins 模块化架构
- 网页HTML5--飞机大战小游戏开发--canvas的应用
- 难免犯傻 难免一根筋啊
- 考研公共课总结与建议(持续更新)
- 历史上神秘消失的10天 | 历史全知道
热门文章
- 移动端下网页border:1px显示
- C#中判断空字符串的3种方法性能分析
- 实现IFrame的自适应高度
- 【数据结构与算法】之深入解析“太平洋大西洋水流问题”的求解思路与算法示例
- Swift之五个让Swift代码更加优雅的扩展
- G6 图可视化引擎——入门教程——图的交互 Behavior
- 2019第十届蓝桥杯C/C++ A组省赛 —— 第三题: 最大降雨量
- 2017年第八届蓝桥杯C/C++ A组国赛 —— 第三题:表达式计算
- 征战蓝桥 —— 2016年第七届 —— C/C++A组第2题——生日蜡烛
- 【C++】Visual Studio教程(一)-概述