C++ 从“int”类型的临时对象初始化“int&”类型的非常量引用无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16767345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalid initialization of non-const reference of type 'int&' from a temporary of type 'int'
提问by dark_shadow
#include<iostream>
using namespace std;
int fun(int &x)
{
return x;
}
int main()
{
cout << fun(10);
return 0;
}
Can anyone explain the reason of the error ?
谁能解释错误的原因?
Thanks
谢谢
回答by paxdiablo
10
is a constant, so you can't pass a reference to it, simply because the whole concept of changing a constant is bizarre.
10
是一个常量,所以你不能传递对它的引用,因为改变常量的整个概念很奇怪。
References were introduced to solve one of the thorny problems in C (and earlier C++), the fact that everything is passed by value and, if you want to have a change reflected back to the caller, you have to pass in a pointer and dereference that pointer within the function to get at the actual variable (for reading and writing to it).
引入引用是为了解决 C(以及更早的 C++)中的一个棘手问题,即一切都是按值传递的,如果您希望将更改反映回调用者,则必须传入一个指针并取消引用函数中的那个指针来获取实际变量(用于读取和写入它)。
This is something that would be seriouslygood to have in the next ISO C standard. While having to use pointers may give some of us a lot of rep on Stack Overflow, it's not doing the C programmers of the world much good :-)
这是什么,这将是严重的好事,在未来的ISO C标准。虽然不得不使用指针可能会让我们中的一些人在 Stack Overflow 上有很多代表,但这对世界上的 C 程序员来说并没有多大好处:-)
The solution to your problem is simple. If you don'tneed to change the item in the function, just pass it normally:
解决您的问题很简单。如果你没有需要改变的功能的项目,只是通过它正常:
int fun (int x) { ... }
If you doneed to change it, well, then you'll have to pass something that canbe changed:
如果你确实需要改变它,那么你必须传递一些可以改变的东西:
int xyzzy = 10;
cout << fun (xyzzy);
回答by 0x499602D2
We can shorten this program down to the following:
我们可以将这个程序缩短为以下内容:
int& x = 10;
The reason this code doesn't work is because 10 is an rvalue, and rvalues cannot bind to lvalue-references. If that was true, we'd be able to change the value of a literal (which is possible in other languages but not in C++).
这段代码不起作用的原因是因为 10 是一个右值,而右值不能绑定到左值引用。如果这是真的,我们将能够更改文字的值(这在其他语言中是可能的,但在 C++ 中是不可能的)。
回答by Daemon
10 here is a const number and you are trying to create a non const reference to it. Either store 10 in some variable like
10 这里是一个常量数字,您正在尝试创建对它的非常量引用。要么将 10 存储在某个变量中,例如
int x = 10
int x = 10
then pass it to function or make reference as const.
然后将其传递给函数或作为 const 引用。
const int &x
Have it been C++11 then you can also use concept of rvalue reference, so it wont give error.
如果是 C++11 那么你也可以使用右值引用的概念,所以它不会出错。
For more details see error: invalid initialization of non-const reference of type ‘int&' from an rvalue of type ‘int'
有关更多详细信息,请参阅错误:从类型为 'int' 的右值对类型为 'int&' 的非常量引用无效初始化
回答by nullptr
A reference (&
) must point (reference) to a variable. A constant cannot be referenced.
引用 ( &
) 必须指向(引用)一个变量。不能引用常量。