传递'int f(int&,int)'的参数1时出错 [英] error in passing argument 1 of 'int f(int& ,int)'

查看:174
本文介绍了传递'int f(int&,int)'的参数1时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这条错误信息的含义。

为什么我会收到此错误?这是编码;



#include< iostream>

使用命名空间std;





int f(int& x,int c);



int main()

{

cout<<测试参考<< f(5,5);



返回0 ;

}





int f(int& x,int c){

c = c - 1;

如果(c == 0)返回1;

x = x + 1;

返回f (x,c)* x;

}

I don't understand the meaning of this error message.
why do I get this error? here is the coding;

#include <iostream>
using namespace std;


int f(int &x, int c);

int main()
{
cout <<" testing reference"<<f(5,5);

return 0;
}


int f(int &x, int c) {
c = c - 1;
if (c == 0) return 1;
x = x + 1;
return f(x, c) * x;
}

推荐答案

您正在传递第一个参数作为参考。这意味着实际的参数应该是一个可以被引用的对象,它不能是一个直接的常量,例如5.通过引用传递允许函数修改这个参数的值,所以它应该是一个变量。



你应该从函数签名中删除'&',或者使用变量作为第一个参数传递。



-SA
You are passing first argument as reference. It means that the actual argument should be an object that can be referenced, it can not be an immediate constant such as 5. Passing by reference allows the function modify the value of this argument, so it should be a variable.

You should either remove '&' from the function signature, or use a variable to pass as the first argument.

—SA


这篇关于传递'int f(int&amp;,int)'的参数1时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆