What is the output of the following snippet? #include <iostream> namespace SpaceA { int A; } namespace SpaceB { int A; } using namespace SpaceA, SpaceB; int main() { SpaceA::A = SpaceB::A = 1; std::cout << A + 1; } 2 0 1 Compilation fails
Question
What is the output of the following snippet?
#include <iostream>
namespace SpaceA { int A; }
namespace SpaceB { int A; }
using namespace SpaceA, SpaceB;
int main() { SpaceA::A = SpaceB::A = 1; std::cout << A + 1; }
2
0
1
Compilation fails
Solution
The compilation fails. The reason is that the compiler doesn't know which 'A' to use when you try to print 'A + 1'. Both 'SpaceA::A' and 'SpaceB::A' are in scope due to the 'using namespace' declarations, so the reference to 'A' is ambiguous.
Similar Questions
What is the output for the following code?
What does the following code snippet do?
What is the output of the following code in Python?
What is the output for the following code?Note: This question helps in clearing conceptual technical interviews for service-based companies.
What will be the output of the following PHP code?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.