我有一个小游戏的代码如下(用CLR写的,不值得一读):
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
using namespace System;
char map[15][15+1]={
"###############",
"#.............#",
"#.............#",
"#....#####....#",
"#.............#",
"#.............#",
"#.............#",
"######...######",
"#.............#",
"#.............#",
"#.............#",
"#....#####....#",
"#.............#",
"#.............#",
"###############"};
int x1,y1,x2,y2;
int val(int x, int y){
if(map[y][x]=='#') return 0;
if(x==x1 && y==y1) return 0;
if(x==x2 && y==y2) return 0;
return 1;
}
int main(array<System::String ^> ^args)
{
int x, y;
while(1){
cin>>x>>y;
if(x<0 || x>=15 || y<0 || y>=15 || map[y][x]=='#')
continue;
break;
}
while(1){
cin>>x1>>y1;
if(x1<0 || x1>=15 || y1<0 || y1>=15 || map[y1][x1]=='#')
continue;
break;
}
while(1){
cin>>x2>>y2;
if(x2<0 || x2>=15 || y2<0 || y2>=15 || map[y2][x2]=='#')
continue;
break;
}
system("cls");
for(int i=0;i<15;++i) cout<<map[i]<<endl;
int d1=1,d2=1,choq=0;
while(1){
Sleep(100);
Console::SetCursorPosition(x,y);
cout<<".";
if(_kbhit()){
int dir=_getch();
if(dir==72){//ar
if(val(x,y-1)) y-=1;
else ++choq;
}
if(dir==75){//izq
if(val(x-1,y)) x-=1;
else ++choq;
}
if(dir==77){//der
if(val(x+1,y)) x+=1;
else ++choq;
}
if(dir==80){//ab
if(val(x,y+1)) y+=1;
else ++choq;
}
if(dir=='s' || dir=='S')
break;
}
Console::SetCursorPosition(x1,y1);
cout<<".";
Console::SetCursorPosition(x2,y2);
cout<<".";
if(map[y1+d1][x1]=='#' || (y1+d1==y && x1==x)){
if(y1+d1==y && x1==x)++choq;
d1=-d1;
}
if(map[y2+d2][x2]=='#' || (y2+d2==y && x2==x)){
if(y2+d2==y && x2==x)++choq;
d2=-d2;
}
y1+=d1;
y2+=d2;
Console::SetCursorPosition(x1,y1);
cout<<"#";
Console::SetCursorPosition(x2,y2);
cout<<"#";
Console::SetCursorPosition(x,y);
cout<<"#";
Console::SetCursorPosition(0,16);
cout<<choq <<" choques.";
}
Console::SetCursorPosition(0,17);
cout<<"Simulacion terminada.";
getchar();
getchar();
return 0;
}
但是当我编译时,我得到错误提示 x1, y1
是 double (_cdecl*)(double)
类型。如果我用 x_1, y_1
替换他们的名字,问题就解决了。但是,为什么会这样呢?我在包含的 header 中的任何地方都找不到该定义,并且当我编写 x1(
时,intelliSense 不会提示我有关参数或类型的信息。此外,Visual Studio 2012 不会提示该行
int x1,y1,x2,y2;
重新定义
你能帮我理解这种行为吗?
最佳答案
它们已被弃用 Posix functions .你从 <math.h>
得到的, 由 <iostream>
拉入.
您找到的解决方法还可以。这些名称选择来自程序员必须用左脚操作 shift 键的时代,计算机仍在与桌面计算器竞争,在全局命名空间中倾倒短标识符名称几乎没有像今天这样大的问题。像“y1”这样的标识符名称适用于参数或局部变量名称。
对于全局变量来说不太好。为全局变量使用描述性名称,您仍然只需要按几个键。编辑器的自动完成功能是您的好 helper 。代码的读者和编译器都会感谢您。
关于visual-studio-2012 - 好奇的名字混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24578870/