次のコードを記述すると、適切にコンパイルおよび実行されます。
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using namespace first; //using derective
using second::y;
cout << x << endl;
cout << y << endl;
return 0;
}
しかし、次のように main 関数の外でディレクティブを使用して記述すると、
using namespace first; //using derective
using second::y;
int main () {
cout << x << endl;
cout << y << endl;
return 0;
}
次のコンパイル エラーが発生します。
g++ namespace03.cpp -o namespace03
namespace03.cpp: In function ‘int main()’:
namespace03.cpp:20:11: error: reference to ‘y’ is ambiguous
namespace03.cpp:13:10: error: candidates are: double second::y
namespace03.cpp:7:7: error: int first::y
make: *** [namespace03] Error 1
main
using ディレクティブが insideと outsideで使用されると動作が異なる理由を誰か説明できますmain
か?