“声明遮盖参数”是什么意思? [英] What does it mean that "a declaration shadows a parameter"?

查看:160
本文介绍了“声明遮盖参数”是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,该函数返回将传递给它的整数的两倍。我的代码收到以下错误消息:

I am trying to make a function that returns double the integer number that I will pass to it. I am getting the following error message with my code:


int x的声明遮盖了参数int x;

declaration of 'int x' shadows a parameter int x; "

这是我的代码:

#include <iostream>
int doublenumber();
using namespace std;
int doublenumber(int x)// <-- this is the function which returns double the value .
{
    int x;
    return 2 * x;
    cout << endl;
}
int main()
{
    int a;
    cout << "Enter the number that you want to double it : " << endl;
    cin >> a;
    doublenumber(a);

    return 0;
}


推荐答案

您将 x 作为参数,然后尝试将其声明为局部变量,这就是有关阴影的抱怨。

You have x as a parameter and then try to declare it also as a local variable, which is what the complaint about "shadowing" refers to.

这篇关于“声明遮盖参数”是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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