关于C++的cannot bind non-const lvalue reference of type…问题

先看下面的代码,一个很简单的切分字符串并输出的函数。

#include <iostream>
using namespace std;
void test(string& str){cout<<str;
}
int main()
{string str = "aabcde";test(str.substr(0,3));}

但当运行的时候会出现下面的错误

这是由于我们的test函数的参数是一个非const引用类型,但是在main()函数中,我们直接调用了str的substr函数,这样相当与把一个临时变量当成参数传递进去,而编译器认为程序员无法对临时变量进行操作,从而产生了错误。

有下面的解决办法:
1、test(string& str) ==> test(const string& str),其余不变。
2、test(string& str) ==> test(string str)。
3、另外定义一个string类型变量接受substr()的返回值。

关于C++的cannot bind non-const lvalue reference of type...问题相关推荐

  1. QT之error: cannot bind non-const lvalue reference of type ‘CBaowen’ to an rvalue of type ‘CBaowen

    问题描述:QT中自己编写了一个结构体变量CBaowen,报文中含有函数重载部分(如下所示), 之后定义一个队列变量 QQueue<CBaowen>  queue; 当给队列变量赋值时que ...

  2. cannot bind non-const lvalue reference of type ‘xxx‘ to an rvalue of type ‘xxx‘

    文章目录 报错代码 报错 原因 解决方案 报错代码 #include<iostream> using namespace std; class Complex{private:double ...

  3. 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 ...

  4. 【报错】关于[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 ...

  5. 【C++---const引用】数组进行指针引用传递给函数error: non-const lvalue reference of type ‘int*‘ to an rvalue

    目录 原因 类型转换 手动转换 自动转换 关于临时量 关于常量引用(const的引用) const引用的对象不是不能被修改了吗,这里为什么被修改了? 不是说是const引用吗?为什么又变成了引用所绑定 ...

  6. 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 ...

  7. C++非const引用问题:error: cannot bind non-const lvalue reference of type

    出现这个问题是因为把一个临时变量传入了函数的引用形参中,简化代码如下: void back(int &i){return; }int main(){back(1); } 应该改为: void ...

  8. 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 ...

  9. error: cannot bind non-const lvalue reference of type

    文章目录 简化后的问题代码 理论原因 解决方案 简化后的问题代码 #include <iostream> using namespace std;class MyArray {public ...

最新文章

  1. 用STL给C++充电:第一部分
  2. 2016_6_27日报
  3. Python 学习之作用域
  4. jdbc mysql 报错 ssl_Mybatis使用JDBC连接数据库报错及解决方案
  5. layer.confirm 询问框 的层遮盖
  6. Java Web学习总结(3)Servlet(二)
  7. Redis数据结构以及对应存储策略
  8. React开发(100):3.0没有image
  9. Centos7利用fpm制作rpm包(fpm安装及使用)
  10. 籍贯怎样填写_公务员考试:什么是考生户籍、生源地和籍贯?
  11. 阿里发布藏经阁计划:一年建成知识引擎服务平台
  12. vue中axios的基本使用,天气查询案例
  13. 再见也是永别——告别即将消逝的雅虎邮箱
  14. APS计划排程和生产排产系统,包含哪些排程算法?
  15. SDUT 第十届校赛H menhera酱那惨不忍睹的数学 【二分图 || 网络流】
  16. 【Linux】yum卸载恢复
  17. 市场部商业计划PPT模板
  18. 电驴服务器搜索文件排序,电驴怎么连接服务器 电驴eMule怎么搜索文件
  19. ps知识点2-吸管、修复、画笔、仿章
  20. 买天猫店网店转让商标过户事项

热门文章

  1. devtool里的7种SourceMap模式
  2. 【Python蓝桥杯】两个月从毫无头绪拿下省二,我经历了什么???
  3. 数栈技术干货:从0到1实现谷歌插件开发探索及应用
  4. 购物单(excel做法)
  5. 时间戳转换,时间钟表
  6. 地理空间距离计算优化_附近的人(转自美团技术博客)
  7. 用 Python 在 Excel 中画出蒙娜丽莎
  8. java连接Access数据库的简单介绍
  9. java web操作Access数据库
  10. java 逻辑或 作用_java逻辑运算符有哪些?逻辑运算符有什么作用?