“声明”阴影“参数”是什么意思。

发布于 2025-01-25 15:06:08 字数 532 浏览 3 评论 0原文

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

声明'int x'阴影一个参数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;
}

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:

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

Here is my code:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

誰ツ都不明白 2025-02-01 15:06:08

您将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.

假情假意假温柔 2025-02-01 15:06:08

我之所以这样做,是因为您的建议非常有帮助,这是最终结果:

#include <iostream>
using namespace std;

int doublenumber(int x)
{
    return 2*x;
}

int main()
{
    int a;
    cout << "Enter the number that you want to double it : " << endl;
    cin>>a;
    int n= doublenumber(a);
    cout << "the double value is : " << n << endl;
    return 0;
}

I did it because your advice was so helpful, and this is the final result :

#include <iostream>
using namespace std;

int doublenumber(int x)
{
    return 2*x;
}

int main()
{
    int a;
    cout << "Enter the number that you want to double it : " << endl;
    cin>>a;
    int n= doublenumber(a);
    cout << "the double value is : " << n << endl;
    return 0;
}
書生途 2025-02-01 15:06:08
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int d = doublenumber(a);

cout << "Double : " << d << endl;

return 0;
}

您的代码有一些问题。您的声明和功能定义死亡不匹配。因此,删除声明无需它。

您正在声明本地X变量内部函数,该函数将阴影您的函数参数。

#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int d = doublenumber(a);

cout << "Double : " << d << endl;

return 0;
}

There are some problem with your code. Your declaration and definition of function dies not match. So remove declaration as no necessity of it.

You are declaring local x variable inside function which will shadow your function arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文