C++引用传递
我正在尝试使用这个简单的代码进行 C++ 引用传递:
#include <iostream>
int square(int &x)
{
return x*x;
}
int main()
{
std::cout<<"Square of 4 is: "<<square(4)<<std::endl;
return 0;
}
但是,当我尝试运行它时,我得到以下内容:
更新
根据 @Pablo Santa Cruz 的回答修改代码后出现以下错误(我只是屏幕截图了部分错误):
这是为什么?
谢谢。
I'm trying C++ pass-by-reference using this simple code:
#include <iostream>
int square(int &x)
{
return x*x;
}
int main()
{
std::cout<<"Square of 4 is: "<<square(4)<<std::endl;
return 0;
}
But, when I try to run it, I get the following:
UPDATE
I get the following error after modifying the code based on @Pablo Santa Cruz's answer (I just screen captured part of the error):
Why is that?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您不能将临时引用传递给非常量引用。
让
square
接受const int &
:You cannot pass temporaries to non-constant references.
Make
square
accept aconst int &
:如果您需要引用,则无法传递常量 (
4
)。您必须传递一个变量:
话虽如此,您可能想要这样的东西:
不是返回 int 的函数。
You can't pass a constant (
4
) if you are expecting a reference.You must pass a variable:
Having said that, you probably want something like this:
Not a function returning an int.
这是因为 C++03 的 § 8.5.3 5:
上面,“cv*”指的是修饰符“const”和“volatile”,“T*”指的是类型名称。例如,
const int
(cv = "const", T = "int")、const volatile std::string
( cv =“常量易失性”,T =“std::string”),char*
(cv=“”,T =“char*”) 。简而言之,rvalues 只允许绑定到 const 引用。
更新
如果您的
square
现在返回 void(代码或它没有发生),那么新错误是因为没有operator<<(std::out&, void)< /代码>。
It's because of § 8.5.3 5 of C++03:
Above, "cv*" refers to the modifiers "const" and "volatile", and "T*" refer to type names. For example,
const int
(cv = "const", T = "int"),const volatile std::string
(cv = "const volatile", T = "std::string"),char*
(cv = "", T = "char*").In short, rvalues are allowed to bind only to const references.
Update
If your
square
now returns void (code or it didn't happen), then the new error is because there is nooperator<<(std::out&, void)
.编译器为常量
4
创建一个临时对象,该对象不能作为非常量引用传递给函数。在main
中创建一个int
对象并将其传递给函数,或者通过常量引用或复制获取函数参数。Compiler creates a temporary object for constant
4
which can not be passed to a function as a non-const reference. Create aint
object inmain
and pass it to the function or take the function parameter by const-reference or by copy.该声明
表示函数
square
可能会更改其参数(尽管事实并非如此)。因此,调用square(4)
是荒谬的:程序需要准备好更改数字 4。正如人们所指出的,您可以更改函数以指定它不会更改其参数:
或者您可以使用可以修改的值来调用原始的
square
。The declaration
says that the function
square
may change its argument (although it doesn't really). So to callsquare(4)
is ridiculous: the program needs to be ready to change the number 4.As people have noted, you can either change the function to specify that it won't change its argument:
Or you can call your original
square
using a value that can be modified.引用必须是左值——基本上,您可以在赋值语句的左侧看到它。
所以基本上,您需要首先分配给
main
中的变量。然后你可以将它传递到square
中。A reference must be an l-value-- basically, something you can see on the left side of an assignment statement.
So basically, you need to assign to a variable in
main
first. Then you can pass it intosquare
.引用是另一个变量的别名。 “4”不是变量,因此您的引用没有任何别名。因此编译器会抱怨。
A reference aliases another variable. "4" is not a variable, and therefore your reference has nothing to alias. The compiler therefore complains.
你想要的是这样的:
你注意到我是如何去掉
int square(int &x)
中的&
了吗?&
表示按引用传递。它需要一个变量。square(4)
中的4
是一个常量。使用引用传递,您可以更改 x 的值:
现在:
尽管如此,我认为您不希望这样。 (你是吗??)第一种方法要好得多。
What you want is this:
Did you notice how I got rid of the
&
inint square(int &x)
? The&
means pass-by-reference. It takes a variable. The4
insquare(4)
is a constant.Using pass-by-reference, you can change the value of x:
Now:
Although, I don't think you want this. (Do you??) The first method is a lot better.