Reputation: 565
I have the following code of a mini-game(written in CLR, not worthy of a complete reading):
#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;
}
But when I compile, I get errors saying that x1, y1
are of type double (_cdecl*)(double)
. If I replace their names by x_1, y_1
the problem is solved. But, why does this happens? I can't find that definition anywhere in the included headers, and intelliSense doesn't hint me about the parameters or type when I write x1(
.Moreover, Visual Studio 2012 doesn't complain about the line
int x1,y1,x2,y2;
being a redefinition.
May you help me to understand this behavior?
Upvotes: 2
Views: 76
Reputation: 942308
They are deprecated Posix functions. You got them from <math.h>
, pulled in by <iostream>
.
The workaround you found is okayish. These name choices came from an era where programmers had to operate the shift key with their left foot, computers were still competing with desktop calculators and dumping short identifier names in the global namespace was not nearly as much of problem as it is today. An identifier name like "y1" is fine for an argument or local variable name.
Not so fine for global variables. Use descriptive names for globals, you still only have to press but a few keys. The editor's auto-completion feature is your friend. The reader of your code will thank you, as does the compiler.
Upvotes: 4
Reputation: 4468
I used to get an error like that frequently in Microsoft Developer Studio 6; it stopped happening when I upgraded to Visual Studio 2010. I don't know about the 2012 version.
There had been a definition for y1 (but not x1) hidden deeply within the headers. Try right-clicking on "y1" and selecting "Show definition" from the popup.
Upvotes: 1