2

I am having trouble understanding namespaces. A.cpp:

#include <iostream>
namespace A { int pause = 8; }
int main() {
    std::cout << A::pause << std::endl;
    return 0;
}

And it prints 8. However, now I add using namespace A and change A::pause to just pause. A.cpp:

#include <iostream>
namespace A { int pause = 8; }
using namespace A;
int main() {
    std::cout << pause << std::endl;
    return 0;
}

Now, I get the compile errors:

A.cpp: In function ‘int main()’:
A.cpp:5: error: reference to ‘pause’ is ambiguous
/usr/include/unistd.h:507: error: candidates are: int pause()
A.cpp:2: error:                 int A::pause
A.cpp:5: error: reference to ‘pause’ is ambiguous
/usr/include/unistd.h:507: error: candidates are: int pause()
A.cpp:2: error:                 int A::pause

Can someone explain what went wrong? I thought using namespace A allows me to omit the A::, similarly to how using namespace std allows you to omit std::. I tried moving the line in main()but I get the same error messages. Note, I purposefully chose the variable name pause as it apparently conflicts with the pause() declared in iostream. Any feedback is appreciated. Thanks!

5
  • It worked when I added using::pause in the beginning of main() Commented Feb 7, 2013 at 13:57
  • 2
    I'd call that a bug in your compiler/library. Including <iostream> should not pollute the global namespace. Commented Feb 7, 2013 at 13:59
  • 1
    Did you enable all your warnings? You might get the wrong pause! Commented Feb 7, 2013 at 14:00
  • 1
    There's not even a standard function named pause(). Commented Feb 7, 2013 at 14:08
  • Yet another example of why using directives should be avoided in general... Commented Feb 7, 2013 at 14:46

4 Answers 4

7

The compiler is telling you what the problem is. It has two possible routes to resolve pause - one is via A::pause and the other is via a function pause() which is defined in <unistd.h> and exists outside any namespace. The compiler can't decide which to use so you have to help it.

If you chose a less generic name for your variable inside the A namespace, e.g. pause_val, you could output it without the namespace scoping

#include <iostream>
namespace A { int pause_val = 8; }
using namespace A;
int main() {
    std::cout << pause_val << std::endl;
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

4

Because there is a system function named pause. If you check the error message more closely you will see it's also defined in the system header <unistd.h>.

The system pause function is in the global namespace, and when you do using namespace A you pull in the symbols from the A namespace into the global namespace as well. So now you have two symbols named pause in the global namespace, which is what the compiler complains about.

It might have worked if you called the function pause, as only functions can be called and not integer variables. However, you can use a function as a variable as well (as function pointers) which leads to this conflict.

Comments

3

As the error message says, after you added the using statement there is more than one thing named pause in the global namespace. That's why using namespace whatever; in global scope is a bad idea.

Comments

3

Can someone explain what went wrong? I thought using namespace A allows me to omit the A::, similarly to how using namespace std allows you to omit std::

Yeah, it does, but then you have all the other things called pause vying for competition, too.

This is why using namespace is oft frowned upon.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.