[Error] name lookup of ‘b’ changed for ISO ‘for’ scoping [-fpermissive] 错误代码解决
我这是在Dev C++内出现的问题,在vc++中据说不会有这种问题,请大家以实际情况为准,希望对大家有所帮助
下面先把错误的源代码给大家展示:
#include <bits/stdc++.h>
using namespace std;
const int M=7;
const int N=5;
void InitChess(char *);
void HumanMove(char *);
void ComputerMove(char *);
void ShowChess(char *);
char WinCheck(char *);
int main()
{
char chess[M*M];
char done;
cout<<"---Game of Chess---"<<endl;
cout<<"Human against the stupid computer"<<endl;
done='*';
InitChess(chess);
do{
ShowChess(chess);
HumanMove(chess);
done=WinCheck(chess);
if(done!='*')
ComputerMove(chess);
done=WinCheck(chess);
}while(done=='*');
ShowChess(chess);
if(done=='H')
cout<<"Human won!"<<endl;
else
cout<<"Computer won!!!"<<endl;
return 0;
}
void InitChess(char chess[])
{
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
chess[i+M*j]='*';
}
void HumanMove(char chess[])
{
int x,y;
cout<<"Enter X,Y coordinates for your move:";
cin>>x>>y;
cout<<endl;
x--;y--;
if(chess[x+M*y]!='*')
{
cout<<"Invalid move,try again."<<endl;
HumanMove(chess);
}
else
chess[x+M*y]='H';
}
void ComputerMove(char chess[])
{
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
if(chess[i+M*j]=='*') break;
if(chess[i+M*j]=='*') break;
}
if(i*j==M*M)
{
cout<<"Diamond cut diamond"<<endl;
exit(0);
}
else chess[i+M*j]='C';
}
void ShowChess(char chess[])
{
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
cout<<chess[i+M*j]<<"\t";
cout<<endl<<endl;
}
}
char WinCheck(char chess[])
{
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
char t=chess[i+M*j];
int count=1;
for(int a=i+1,b=j+1;chess[a+M*b]==t&&a<M&&b<M;a++,b++)
count++;
if(count>=N)
return t;
count=1;
for(a=i,b=j+1;chess[a+M*b]==t&&b<M;b++)
count++;
if(count>=N)
return t;
count=1;
for(a=i+1,b=j;chess[a+M*b]==t&&a<M;a++)
count++;
if(count>=N)
return t;
count=1;
for(a=i+1,b=j-1;chess[a+M*b]==t&&a<M&&b>=0;a++,b--)
count++;
if(count>=N)
return t;
count=1;
}
}
return '*';
}
这是编译时的错误提示:
C:\Users\Administrator\Desktop\6.cpp In function ‘char WinCheck(char*)’:
92 8 C:\Users\Administrator\Desktop\6.cpp [Error] name lookup of ‘a’ changed for ISO ‘for’ scoping [-fpermissive]
92 8 C:\Users\Administrator\Desktop\6.cpp (if you use ‘-fpermissive’ G++ will accept your code)
92 12 C:\Users\Administrator\Desktop\6.cpp [Error] name lookup of ‘b’ changed for ISO ‘for’ scoping [-fpermissive]
错误的原因是在Dev C++中无法识别这种定义方式,在for语句中定义的元素在此循环外无法使用
修改方法:
for(int j=0;j<M;j++)
{
char t=chess[i+Mj];
int count=1;
for(int a=i+1,b=j+1;chess[a+Mb]==t&&a<M&&b<M;a++,b++)
//改为:
for(a=i+1,b=j+1;chess[a+Mb]==t&&a<M&&b<M;a++,b++)
count++;
if(count>=N)
return t;
count=1;
for(a=i,b=j+1;chess[a+Mb]==t&&b<M;b++)
count++;
if(count>=N)
return t;
count=1;
for(a=i+1,b=j;chess[a+Mb]==t&&a<M;a++)
count++;
if(count>=N)
return t;
count=1;
for(a=i+1,b=j-1;chess[a+Mb]==t&&a<M&&b>=0;a++,b–)
count++;
if(count>=N)
return t;
count=1;
}