When I try to compile this code:
#include <iostream>
namespace Direction
{
enum Enum { UP, DOWN, LEFT, RIGHT };
}
using namespace std;
void move(int pDir);
int main()
{
printf("UP: %u\n", Direction::UP);
printf("DOWN: %u\n", Direction::DOWN);
printf("LEFT: %u\n", Direction::LEFT);
printf("RIGHT: %u\n", Direction::RIGHT);
move(Direction::UP);
return 0;
}
void move(int pDir)
{
printf("Move: ");
switch(pDir)
{
case(Direction::UP):
printf("UP");
break;
case(Direction::DOWN):
printf("DOWN");
break;
case(Direction::RIGHT):
printf("RIGHT");
break;
case(Direction::LEFT):
printf("LEFT");
break;
default:
printf("nothing");
break;
}
}
The result I expect would be:
UP: 0
DOWN: 1
LEFT: 2
RIGHT: 3
Move: UP
Instead the result is:
UP: 0
DOWN: 1
LEFT: 2
RIGHT: 3
It seems like void move(..) just gets ignored.
I already found the problem: It's the using namespace std. When I delete it, I get the result as expected.
So I have three questions:
1) Why does void move(..) just gets "ignored"
2) Why can I access the members of Direction within the first four lines of int main()
3) How can I fix this? ,_,
Have a nice day my friends.
ps: This is an extracted example of my problem, on my project I need to use the namespace std.
using namespace std;, there is alsostd::move, do you know which one it is when you writemove? ;)printfs)usingstd::moveyou again cannot easily spot the difference betweenstd::moveor::move(btw::movemight fix your problem, but I am not sure at all)std::cout. explicit > implicit. Those few characters aren't that hard to type. Eventually, if in a specific scope you use an identifier from::stda lot, you shouldusing std::identifierto save some typing. But in general, just be explicit and help your future self ;)