c++ - 错误 : name lookup of ‘x’ changed for ISO ‘for’ scoping

标签 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;
  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;
}

最佳答案

x 的范围仅限于 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;

}

关于c++ - 错误 : name lookup of ‘x’ changed for ISO ‘for’ scoping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24389952/

相关文章:

c++ - 通过传递参数进行字符串连接

c++ - wxButton - 使用 C++ 改变位置

c++ - C++中有符号到无符号的转换

c++ - 将两个 vector 对转换为相应元素的映射

c++ - 为什么 std::condition_variable 作为类成员会导致 std::thread 出现编译错误?

c++ - 如何测试我的软件是否能正确使用 UTF-16?

c - 读取文件夹或目录中的所有文件

c - 使用带有整数的scanf的段错误

C - Eclipse CDT - 高效调试 + 什么是更好的代码(函数指针)?

c++ - 使用模板应用功能模板