invalid initialization of non-const reference of type ‘xxx&’ from an rvalue of type ‘xxx’
来源:互联网 发布:小罗伯特唐尼 知乎 编辑:程序博客网 时间:2024/06/06 10:57
这是我在学习c++的过程中曾经遇到过的一条Linux环境下的编译错误
一开始不知道这个报错的意思是什么,debug半天没de出来,在网上阅博百篇,总算知道了这个报错的意思,一下就找出bug了。
因此直接将该报错信息作为标题以便后人搜索查阅。
- 这个报错的中文意思是:非常量引用的初始值必须为左值(vs中的报错)
- 最常见的原因有两种:
声明了一个针对常量的引用,例如
#include <iostream>using namespace std;int main(){ int& i = 10;//这种通常在vs里报错为:非常量引用的初始值必须为左值 /*在Linux便是invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’*/ return 0;}
解决办法
#include <iostream>using namespace std;int main(){ int x = 10; int& i = x; return 0;}
在参数为非常量引用类型的函数中传入常量类型,例如
#include <iostream>using namespace std;void fun(int& i){ cout << i;}int main(){ fun(1);//这种通常在vs里报错为:非常量引用的初始值必须为左值 /*在Linux便是invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’*/ return 0;}
又如
#include <iostream>#include <string>using namespace std;void fun(string& i){ cout << i;}int main(){ fun("str");//这种通常在vs里报错为:非常量引用的初始值必须为左值 /*在Linux便是invalid initialization of non-const reference of type ‘string&’ from an rvalue of type ‘string’*/ return 0;}
解决办法均为在参数前面加个const关键字
#include <iostream>#include <string>using namespace std;void fun(const int& i){ cout << i;}int main(){ fun(1); return 0;}
#include <iostream>#include <string>using namespace std;void fun(const string& i){ cout << i;}int main(){ fun("str"); return 0;}
所以,写代码养成随手加const是个好习惯,扼杀bug于编译的摇篮中。
阅读全文
0 0
- invalid initialization of non-const reference of type ‘xxx&’ from an rvalue of type ‘xxx’
- C++引用报错:invalid initialization of non-const reference of type ‘std::string&’ from an rvalue of type
- C++之invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
- invalid initialization of non-const reference of type ‘int*&’ from a temporary of type ‘int*’
- 编译错误:error: invalid initialization of non-const reference of type ‘*&’ from a temporary of type
- error: invalid initialization of non-const reference of type ‘*&’ from a temporary of type
- invalid initialization of non-const reference of type与discards qualifiers
- crosses initialization of “XXX”
- crosses initialization of “XXX”
- “Bean named XXX must be of type[XXX], but was actually of type[XXX]”
- “Bean named XXX must be of type[XXX], but was actually of type[XXX]”
- Error: Expected resource of type xxx [ResourceType]
- Cannot initialize a parameter of type 'NSString *_Nullable' with an rvalue of type ''UITouchPhase"
- cannot initialize a parameter of type 'IPCameraNightMode' with an rvalue of type 'int'
- initialization of xxx is skipped by xxx
- identifier of an instance of XXX was altered from 75
- Type Deduction && Initialization of variables
- error C2275: 'xxx' : illegal use of this type as an expression
- Atitit 支出减少之道 attilax总结 适用于个人与 组织
- 静态成员
- Word空白处下划线怎么打不出来
- SpringMVC 传入参数(@PathVariable)乱码
- 确定进程被锁住的其他资源
- invalid initialization of non-const reference of type ‘xxx&’ from an rvalue of type ‘xxx’
- 重构8.11-8.16
- flume01简介及安装部署
- Python 爬虫爬取图片(未分页)
- 百度地图API之公交换乘方案搜索(Android)
- 百度地图API之城市POI搜索-获取所有结果(Android)
- 教你轻松在React Native中集成统计的功能
- 百度地图API之POI搜索-发现你身边的兴趣点,如超市、餐厅、ATM...(Android)
- 百度地图API之根据经纬度查询地址信息(Android)