Global variable error

Why does it not work when i try to make the array global variable it works ok when it's a local variable of main here's an example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char floor[20][20];
 int main()
{
	
	for(int c=0;c<20;c++)
			 {
				 for(int r=0;r<20;r++)
				 {
					std:: cout<<floor[r][c];
				 }
				std:: cout<<std::endl;
			 }
	     
	
	getch();
  return 0;
}

this does not work and this does
int main()
{
char floor[20][20];
for(int c=0;c<20;c++)
{
for(int r=0;r<20;r++)
{
std:: cout<<floor[r][c];
}
std:: cout<<std::endl;
}


getch();
return 0;
}
What doesn't work? Are you getting compiler errors?
Please explain yourself better and provides us the compile log so we can understand your problem.

PS: Why aren't you defining the array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char floor[20][20];
 int main()
{
	
	for(int c=0;c<20;c++)
			 {
				 for(int r=0;r<20;r++)
				 {
					std:: cout<<floor[r][c];
				 }
				std:: cout<<std::endl;
			 }
	     
	
	getch();
  return 0;
}

Does this work on your compiler i get an error:
Error 1 error C2365: 'floor' : redefinition; previous definition was 'function'
Error 2: error C2563: mismatch in formal parameter list

johny31 defining array shouldn't matter , doesn't it?
In g++ it gives no errors however it doesn't display anything, it just creates a big space with whitespace which is normal since the arrays isn't defined.

What compiler are you using? Visual Studio? If so which version?

EDIT: Try removing getch(); and substitute it for std::cin.ignore();
Last edited on
> Why does it not work when i try to make the array global variable
> it works ok when it's a local variable

There is already a 'floor' in the global namespace. (library function)
http://www.cplusplus.com/reference/cmath/floor/

There is a name clash; give your array a different name.
For instance char my_floor[20][20];
Didn't made a difference adding cin.ignore(); and removing getch();
Compiler is Visual Studio 2010 express
Strange :/
@JLBorges In the Visual Studio error definition it said that it happened due to a named clash but I wasn't aware of such function in the std library.
Thank you :)
> I wasn't aware of such function in the std library.

Well, the standard library function is std::floor(), the name floor is in the namespace std.
http://en.cppreference.com/w/cpp/numeric/math/floor

However, floor() is in the header <cmath>, the C++ equivalent of the C header <math.h>.
For such headers, an implementation is allowed to first declare the name in the global unnamed namespace scope (just as C does) and then inject the name into the std namespace with a using ::floor ;.
Last edited on
Topic archived. No new replies allowed.