[Error] name lookup of ‘b‘ changed for ISO ‘for‘ scoping [-fpermissive] 错误代码解决

[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+M
b]==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+M
b]==t&&a<M&&b>=0;a++,b–)
count++;
if(count>=N)
return t;
count=1;
}

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值