C++ Error : initial value of reference to non-const must be an lvalue
2014-12-04 08:38
573 查看
如下这段代码,编译报错:
Error : initial value of reference to non-const must be an lvalue
原因为test函数的参数是个引用, 你有可能修改这个引用本身的值(不是引用指向的那个值),这是不允许的。
所以一个修复办法为声明引用为const,这样你告诉编译器你不会修改引用本身的值(当然你可以修改引用指向的那个值)
修改版1:
另一个更简单的修改办法是直接传递指针,
修改版2:
reference:
http://stackoverflow.com/questions/17771406/c-initial-value-of-reference-to-non-const-must-be-an-lvalue
Error : initial value of reference to non-const must be an lvalue
#include <iostream> using namespace std; void test(float *&x){ *x = 1000; } int main(){ float nKByte = 100.0; test(&nKByte); cout << nKByte << " megabytes" << endl; }
原因为test函数的参数是个引用, 你有可能修改这个引用本身的值(不是引用指向的那个值),这是不允许的。
所以一个修复办法为声明引用为const,这样你告诉编译器你不会修改引用本身的值(当然你可以修改引用指向的那个值)
修改版1:
void test(float * const &x){ *x = 1000; }
另一个更简单的修改办法是直接传递指针,
修改版2:
void test(float * x){ *x = 1000; }
reference:
http://stackoverflow.com/questions/17771406/c-initial-value-of-reference-to-non-const-must-be-an-lvalue
相关文章推荐
- Error: initial value of reference to non const must be lvalue 原因以及解决方法
- C++引用报错:invalid initialization of non-const reference of type ‘std::string&’ from an rvalue of type
- caffe: fuck compile error again : error: a value of type "const float *" cannot be used to initialize an entity of type "float *"
- C++之invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
- keil(MDK)中出现error: #513: a value of type "int" cannot be assigned to an entity of type "char *",即函数返回值无法赋值给对应变量的错误
- could not be set to a 'string' value. You must set this property to a non-null value of type
- C++引用报错:invalid initialization of non-const reference of type ‘std::string&’ from an rvalue of type
- keil(MDK)中出现error: #513: a value of type "int" cannot be assigned to an entity of type "char *",即函数返
- 类似Build Error: #513: a value of type "int" cannot be assigned to an entity of type "char *"
- invalid initialization of non-const reference of type ‘xxx&’ from an rvalue of type ‘xxx’
- a value of type "const struct Menu * "cannot be assigned to an entity of type "struct Menu"
- When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.
- Sys.ArgumentOutOfRangeException: Value must be an integer 错误的原因
- Error with Stsadm Command-Object reference not set to an instance of an object
- iOS中使用opencv碰到这样的编译错误:non-const static data member must be initialized out of line
- 读书笔记_Effective_C++_条款二十:宁以pass-by-reference-to-const替换pass-by-value
- Sys.ArgumentOutOfRangeException: Value must be an integer
- Submit Reporting Table Error : Object reference not set to an instance of an object.
- Sys.ArgumentOutOfRangeException: Value must be an integer 错误的原因 iframe
- 20100507 学习记录: Server Error in '/' Application. Object reference not set to an instance of an object