C++ 无法将参数从“int”转换为“int&”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13555556/
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
Cannot convert parameter from 'int' to 'int&'
提问by Garth Marenghi
I'm following the pluralsight C++ course, and in it is the following code:
我正在学习复数 C++ 课程,其中包含以下代码:
#include <iostream>
template <class T>
T max(T& t1, T& t2)
{
return t1 < t2 ? t2 : t1;
}
int main()
{
std::cout << "Max of 33 and 44 is " << max(33, 44) << std::endl;
return 0;
}
I typed over this piece of code, but unlike the code of the course, I get an error message:
我输入了这段代码,但与课程代码不同的是,我收到一条错误消息:
C2664: 'max' : cannot convert parameter 1 from 'int' to 'int &'
The code in the course is written in Visual Studio Express 2010, while mine is written in Visual Studio Ultimate 2010.
课程中的代码是用 Visual Studio Express 2010 编写的,而我的代码是用 Visual Studio Ultimate 2010 编写的。
EDIT
编辑
Thanks to everybody (even Kate Gregory herself) for providing answers and clearing everything up.
感谢大家(甚至凯特格雷戈里本人)提供答案并清理一切。
回答by
Because literals (and rvalues in general) cannot be passed by non-const reference (since it would make no sense if the callee could change them). Either pass by value or by const reference:
因为文字(和一般的右值)不能通过非常量引用传递(因为如果被调用者可以改变它们就没有意义)。通过值或通过常量引用传递:
template <class T>
T max(const T& t1, const T& t2)
{
return t1 < t2 ? t2 : t1;
}
or
或者
template <class T>
T max(T t1, T t2)
{
return t1 < t2 ? t2 : t1;
}
回答by Kate Gregory
The answer here turns out to lie in the unrelated code you didn't use in your test. Absolutely my little max() should take either by value, or const-ref, and that was just a brain-slip that I will fix as soon as I can.
事实证明,这里的答案在于您没有在测试中使用的无关代码。绝对我的小 max() 应该按值或 const-ref 取值,这只是一个我会尽快修复的脑失误。
But the full demo code works beautifully. That's because it includes more headers, one of which brings in xutility and therefore std::max
. This led me to not notice that my max
wasn't being used. I'll redo the demo with a name like biggest
to eliminate that happening again. And in the meantime, yes, if you want to pass literals to a function by reference, they need to be const ref. I knew that, but didn't think of it while writing the test harness, and then was fooled when bad code appeared to work. I should have re-read the code more carefully: thanks for calling this to my attention. (And thanks for taking the course and I hope you're finding it helpful.)
但完整的演示代码运行良好。那是因为它包含更多的标头,其中之一引入了 xutility,因此std::max
. 这导致我没有注意到我的max
未被使用。我将重做演示,名称类似于biggest
消除这种情况再次发生。同时,是的,如果您想通过引用将文字传递给函数,则它们需要是 const ref。我知道这一点,但在编写测试工具时没有想到这一点,然后当糟糕的代码似乎可以工作时被愚弄了。我应该更仔细地重新阅读代码:感谢您提醒我注意这一点。(感谢您参加这门课程,希望对您有所帮助。)
回答by 0x499602D2
Both 33 and 44 are rvalues; they're passed to the function by value and not by reference. The compiler can't convert these two to the expected type int &
. Use variables (lvalues) as they can be passed by reference:
33 和 44 都是右值;它们通过值而不是通过引用传递给函数。编译器无法将这两个转换为预期的 type int &
。使用变量(左值),因为它们可以通过引用传递:
int a = 33, b = 44;
max(a, b); // 44
Since you're merely dealing with fundamental types here (int
), passing by reference is redundant. Passing by value causes a copy, but the difference will be negligible:
由于您在这里仅处理基本类型 ( int
),因此通过引用传递是多余的。按值传递会导致复制,但差异可以忽略不计:
template <class T>
T max(T t1, T t2);
Here we can pass both rvalues and lvalues. But there isthe chance that you could pass in a object of class type. In that case passing by reference is recommended.
在这里,我们可以传递右值和左值。但是有是,你可以在类类型的对象传递的机会。在这种情况下,建议通过引用传递。
But for the case where we don't want a copy and want both lvalues and rvalues, we can pass by const
reference:
但是对于我们不想要副本并且想要左值和右值的情况,我们可以通过const
引用传递:
template <class T>
T max(T const &t1, T const &t2);
In C++11 you can pass the arguments by a universal-referenceso that we can bind glvalues:
在 C++11 中,您可以通过通用引用传递参数,以便我们可以绑定泛左值:
template <class T>
T max(T&& t1, T&& t2);
int main()
{
int a{0}, b{0};
max(a, b);
max(1, 2);
}
If you want to keep the lvalue-reference syntax in your original code, you can use this lvalue shim:
如果要在原始代码中保留左值引用语法,可以使用以下左值填充:
template<typename T>
T& lvalue(T&& t)
{
return t;
}
int main()
{
max(lvalue(1), lvalue(2));
}
回答by RiaD
You can't pass temporary value as T&
. Since you don't change arguments, use const T&
instead
您不能将临时值作为T&
. 由于您不更改参数,请const T&
改用
BTW, there is built-in max
function
顺便说一句,有内置max
功能
回答by Maciek
Integer literals cannot be passed as non-const reference. Besides, your max
function can work with const
references, so mark them appropriately:
整数文字不能作为非常量引用传递。此外,您的max
函数可以使用const
引用,因此请适当标记它们:
T max(const T& t1, const T& t2)
回答by mvp
If you defined your function with parameters by reference, you cannot use anything but variable names for said parameters - and you try to use numbers.
如果您通过引用定义了带有参数的函数,则除了变量名之外,您不能为所述参数使用任何其他名称 - 并且您尝试使用数字。
回答by SergV
Small remark. You can use your initial code of function "max". Try change call from max(34, 44)
to max<const int>(34,44)
. Sometime it can be useful.
小备注。您可以使用函数“ max”的初始代码。尝试将呼叫从 更改max(34, 44)
为max<const int>(34,44)
。有时它可能很有用。
#include <iostream>
template <class T>
T max(T& t1, T& t2)
{
return t1 < t2 ? t2 : t1;
}
int main()
{
std::cout << "Max of 33 and 44 is " << max<const int>(33, 44) << std::endl;
return 0;
}