Source of namespace2.cpp


  1: //namespace2.cpp
  2: //Illustrates that using namespace directives for namespace that each contain
  3: //a declaration of the same variable does not cause a problem unless that 
  4: //variable is actually used, but using namespace declarations of the same
  5: //variable that occurs in two or more namespaces will not compile.

  7: #include <iostream>
  8: #include <iomanip>
  9: using namespace std;

 11: #include "utilities.h"
 12: using Scobey::Pause;

 14: namespace MyStuff
 15: {
 16:     int n = 5; //This is MyStuff::n.

 18: }

 20: namespace YourStuff
 21: {
 22:     int n = 10;  //This is YourStuff::n.
 23: }

 25: int main()
 26: {
 27:     int k = 20;
 28:     cout << "\nThe value of k is " << k << ".\n";
 29:     Pause(0, "", 1);

 31:     //using namespace MyStuff;    //1
 32:     //using namespace YourStuff;  //2
 33:     //using MyStuff::n;           //3
 34:     //using YourStuff::n;         //4

 36:     //cout << "\nThe value of n is " << n << ".\n"; //5
 37:     //Pause(0, "", 2);                              //5
 38: }

 40: /*
 41: Questions:
 42: 1. What values or error are output if the program is run as is?
 43: 2. What values or error are output if comments //1 and //2 are activated?
 44: 3. What values or error are output if comments //1, //2, //5 are activated?
 45: 4. What values or error are output if comments //3 and //4 are activated?

 47: Answers:
 48: 1. 20
 49: 2. 20
 50: 3. error C2872: 'n' : ambiguous symbol
 51: 4. error C2874: using-declaration causes a multiple declaration of
 52:    'YourStuff::n'
 53: */