"a declaration shadows a parameter"是什么意思?
What does it mean that "a declaration shadows a parameter"?
我试图使一个函数,返回两倍的整数,我将传递给它。我得到以下错误信息与我的代码:
下面是我的代码: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;
}
您将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 n= doublenumber(a);
cout << "the double value is : " << n << endl;
return 0;
}
#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,它会遮蔽你的函数参数。
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- "类模板示例<int>;"语句对 C++11 是什么意思?
- 线应该是什么意思
- "CID"在AT+HTTPPARA= "CID" ,1中是什么意思
- 表达式"b=(b-x)&x"是什么意思?
- 这个表达是什么意思?
- 此代码验证公式是什么意思?
- 跟踪日志中的T.11803()是什么意思?
- 这个额外的关键字在这个 c++ 类声明中是什么意思?
- * 和 ** 在 C++ 函数声明中是什么意思?
- _T("xyz")是什么意思?
- #define Dbg(fmt,..) (0) 是什么意思? 警告:表达式无效
- 这行代码到底是什么意思?
- 当我们说在执行程序时将操作系统的控制权传递给main()函数时,我们的意思是什么
- 这句话在c++中的意思是什么
- 这个typedef的意思是什么
- 这句话的意思是什么
- 意思是什么
- 这个句法的意思是什么?