C++ 错误:查找‘;x’;更改为ISO‘;对于’;范围界定
我正在尝试编译我的代码,但出现以下错误:C++ 错误:查找‘;x’;更改为ISO‘;对于’;范围界定,c++,c,c++11,C++,C,C++11,我正在尝试编译我的代码,但出现以下错误: error: name lookup of ‘x’ changed for ISO ‘for’ scoping [-fpermissive] note: (if you use ‘-fpermissive’ G++ will accept your code) 有人能帮我吗。提前谢谢 int main() { int a,m; cout << "Enter values of a and m:" << endl;
error: name lookup of ‘x’ changed for ISO ‘for’ scoping [-fpermissive]
note: (if you use ‘-fpermissive’ G++ will accept your code)
有人能帮我吗。提前谢谢
int main() {
int a,m;
cout << "Enter values of a and m:" << endl;
cin >> a >> m;
a %= m;
for(int x = 1; x < m; x++)
{
if((a*x) % m == 1)
return x;
}
cout << "the value of x" << x << endl;
}
intmain(){
int a,m;
cout a>>m;
a%=m;
对于(int x=1;x cout问题在于变量x
实际上是for
循环的局部变量,不能在循环之外使用
+明显地有一个扩展允许使用“索引”变量在循环之外,但是你必须添加<代码> -fimistable< /Cord>标志允许它。但是请注意,<代码> -fimisty< /COD>标志也允许其他不是“正确”C++的东西。 < p>这是有问题的行:
cout <<"the value of x"<<x<<endl;
coutx
的范围仅限于for循环,因为您在那里声明了它。如果您想在外部使用它,请在适当的范围内声明它,如下所示:
int main() {
int a,m;
cout << "Enter values of a and m:" << endl;
cin >> a >> m;
a %= m;
int x;
for(x=1; x < m; x++)
{
if((a*x) % m == 1)
return x;
}
cout <<"the value of x"<<x<<endl;
}
intmain(){
int a,m;
cout a>>m;
a%=m;
int x;
对于(x=1;x cout您的x
仅在循环内声明,您甚至试图在循环外使用它(cout
的最后一行)。编译器不太喜欢它。我无法获得x的任何输出,可能是什么问题。如果((a*x)%m==1)返回x;
一定是罪魁祸首。您确定要在cout之前返回吗?